+ Reply to Thread
Results 1 to 24 of 24

Pulling out the latest records ONLY. VBA

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Pulling out the latest records ONLY. VBA

    Hi there,

    I have attached the file.

    What I would like to do:

    For each record in column A of "test" worksheet, I want to keep the the ones with the latest date. So in "output" worksheet, you will see ONLY those records with the latest date.

    Sometimes, there can be up to 3 or 4 lines for the same record, with each line showing different dates, but I only want the latest date, which is the last one for the record.

    Can this be done using a macro of some sort?

    Guidance or comment is much appreciated.

    Thanks
    Attached Files Attached Files
    Last edited by Lifeseeker; 12-13-2012 at 11:41 AM.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    Give this a try

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    wow...amazing...it worked.

    If you don't mind walking me through the code...I would like to see if I fully understand how the code works. I am new to the scripting.dictionary...

    Please Login or Register  to view this content.
    . What does 1 represent? 1 is just the normal default code used in this example?

    What does
    Please Login or Register  to view this content.
    mean?
    In this code,
    Please Login or Register  to view this content.
    , what does "item" mean? and how does ";" work?

    in the next line, what does "Split" work?

    I think if I can understand some of these keywords, then I can find my way through easier.

    Hope you can help.
    Thanks!

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    What does 1 represent? 1 is just the normal default code used in this example?
    If the CompareMode is TextCompare (1) the comparison is case-insensitive, while it is case-sensitive if the CompareMode is BinaryCompare (0).

    What does .exists(a(i,1)) mean?
    Use it to check if a given key is already in use in the dictionary

    .Item(a(i, 1)) = a(i, 1) & ";" & a(i, 2)
    .item(key) = a(i, 1) & ";" & a(i, 2) Value for that key. ";" Is used like columns. "AB1/12";"1-Jul-12"




    Here's an good link
    http://www.vbusers.com/code/codeget....D=524&PostID=1

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    Hi,

    I still can't seem to wrap my head around the use of ";" in there.
    Please Login or Register  to view this content.
    Are we treating values in two columns together as one object? "AB1/12" together with "1-Jul-12"?

  6. #6
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    Anybody can help answer my question?

    thanks

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    Hi, can anybody help out?

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    See if this help

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Pulling out the latest records ONLY. VBA

    Here's a different approach

    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    Yet anoher way
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    Hi,
    So it does look like we are kind of merging the cell in
    Please Login or Register  to view this content.
    One more question: what does
    Please Login or Register  to view this content.
    does?

    I always have problems visualizing how Resize() function does. I understand that we are outputting the results starting at A2.

    Thanks

  12. #12
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    .Cells(i + 1, 1).Resize(, 2) = Split(a(i), ";")
    When we split a(i) we will have 2 columns. So Resize(0 Rows,2 Columns)
    1 = AB1/12 ";" 2 = 7/1/2012


    And here's another good link

    http://www.experts-exchange.com/Soft...ss-in-VBA.html

  13. #13
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    In
    Please Login or Register  to view this content.
    , why are we offsetting at the end?

    I can see that this scripting dictionary object is powerful. It's helpful to master it.

    Thanks

  14. #14
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    There reason for offset at the end is to make the Range("a2:b and whatever is the last row is in column A). I didnt know if column A and B would have the same last row so I get the lastrow in column A and use offset(0 rows, 1 column)

    Please Login or Register  to view this content.
    If Column A and Column B would have the same last row then we could use

    Please Login or Register  to view this content.

  15. #15
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    If I have columns in between A and C, and there are other columns like shown in this attached file, and I still want to achieve the same thing, how will that change the code? Are we talking about significant changes?

    Thanks
    Attached Files Attached Files

  16. #16
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    And what would you want the output to be? The same columns as before or include all columns?

  17. #17
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    Include all columns please.

    The Col A and Col C are really just there to help identify which records we want to keep and which records we want to delete. So it's the same principle as before, but has more columns to be put into the array.

  18. #18
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    Something like this. Not to much modifictions

    Please Login or Register  to view this content.

  19. #19
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    In
    Please Login or Register  to view this content.
    what does that little 3 in the bracket mean?

  20. #20
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    Please Login or Register  to view this content.
    Actually the 3 needs to be 2 my bad. Because when you split the item it is 0 based array.

    0 ; 1 ; 2 ; 3 ; 4
    AB1/12;names;29-Jul-12;gender;test

    (0) = AB1/12
    (1) = names
    (2) = 29-Jul-12
    (3) = gender
    (4) = test

  21. #21
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    I noticed also you have this in the worksheets module. I would remove it from there and place it into a standard Module.

  22. #22
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    so if the actual date column is in E, then it would be (4), correct?

  23. #23
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Pulling out the latest records ONLY. VBA

    Yes your correct the split would be
    Please Login or Register  to view this content.

  24. #24
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Pulling out the latest records ONLY. VBA

    I really appreciate your help and taking your time to explain everything to me. Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1