+ Reply to Thread
Results 1 to 24 of 24

Pulling out the latest records ONLY. VBA

Hybrid View

  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

    Sub abc()
     Dim a, i As Long
     
     With Worksheets("test")
        a = .Range("a1", .Cells(Rows.Count, "a").End(xlUp).Offset(, 1))
     End With
     
     With CreateObject("scripting.dictionary")
        .comparemode = 1
        For i = 1 To UBound(a)
            If Not .exists(a(i, 1)) Then
                .Item(a(i, 1)) = a(i, 1) & ";" & a(i, 2)
            Else
                If Split(.Item(a(i, 1)), ";")(1) > a(i, 2) Then
                    .Item(a(i, 1)) = a(i, 1) & ";" & a(i, 2)
                End If
            End If
        Next
        a = .items
     End With
     
     With Worksheets("output")
        .cells.delete
        For i = 0 To UBound(a)
            .Cells(i + 1, 1).Resize(, 2) = Split(a(i), ";")
        Next
     End With
    End Sub
    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...

    .comparemode = 1
    . What does 1 represent? 1 is just the normal default code used in this example?

    What does
    .exists(a(i,1))
    mean?
    In this code,
     .Item(a(i, 1)) = a(i, 1) & ";" & a(i, 2)
    , 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.
    If Not .exists(a(i, 1)) Then
                .Item(a(i, 1)) = a(i, 1) & ";" & a(i, 2)
    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

        First time it finds AB1/12
        If Not .exists(AB1/12) Then
            .Item(AB1/12) = AB1/12 & ";" & 7/1/2012
        Else Second time it finds AB1/12
            '->(0)AB1/12";"7/1/2012 '<--(1)				
            If Split(.Item(AB1/12), ";")(1) > 7/29/2012 Then
                .Item(AB1/12) = AB1/12 & ";" & 7/29/2012
            End If
        End I

  9. #9
    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
    .Item(AB1/12) = AB1/12 & ";" & 7/1/2012.
    One more question: what does
    .Cells(i + 1, 1).Resize(, 2) = Split(a(i), ";")
    does?

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

    Thanks

  10. #10
    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

    Sub ForFUn()
    Dim wsOutput As Worksheet
    Dim LR As Long
    
    ThisWorkbook.Worksheets("Test").Copy , Worksheets(Sheets.Count)
    
    Set wsOutput = Worksheets(Sheets.Count)
    With wsOutput
        .Cells(1, 1).CurrentRegion.Copy .Cells(1, 5)
    LR = .Cells(Rows.Count, 5).End(xlUp).Row
        .Range("B2:B1000").ClearContents
        .Columns("A").RemoveDuplicates 1, xlYes
        .Range("B2").FormulaArray = "=MAX(IF($E$2:$E$" & LR & "=A2,$F$2:$F$" & LR & "))"
        .Range("B2").AutoFill .Range("B2:B" & .Cells(Rows.Count, 1).End(xlUp).Row)
        .Columns("B").Copy
        .Columns("B").PasteSpecial xlPasteValues
        .Columns("E:F").Delete
    End With
    
    Application.CutCopyMode = False
    End Sub
    Attached Files Attached Files

  11. #11
    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
    Sub Abc_v2()
    Dim LR As Long
    Dim ws As Worksheet
    
    Set ws = Worksheets("output")
    
    Application.ScreenUpdating = False
    With Worksheets("Test").Range("a1")
        .CurrentRegion.Copy ws.Range("a1")
    End With
    
    With ws
        .Cells.WrapText = False
        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=ws.Range("B1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortTextAsNumbers
            .SetRange ws.Range("A1").CurrentRegion
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        .Range("a1").CurrentRegion.RemoveDuplicates 1, xlYes
        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=ws.Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            .SetRange ws.Range("A1").CurrentRegion
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
    Application.ScreenUpdating = True
    End Sub

  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
    a = .Range("a1", .Cells(Rows.Count, "a").End(xlUp).Offset(, 1))
    , 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)

    a = .Range("a1", .Cells(Rows.Count, "a").End(xlUp).Offset(, 1))
    If Column A and Column B would have the same last row then we could use

    a = .Range("a1", .Cells(Rows.Count, "b").End(xlUp))

  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

    Option Explicit
    
    Sub abc()
     Dim a, i As Long
     
     With Worksheets("test")
        a = .Range("a1", .Cells(Rows.Count, "e").End(xlUp))
     End With
     
     With CreateObject("scripting.dictionary")
        .comparemode = 1
        For i = 1 To UBound(a)
            If Not .exists(a(i, 1)) Then
                '.Item(a(i, 1)) = a(i, 1) & ";" & a(i, 2) & ";" & a(i, 3) & ";" & a(i, 4) & ";" & a(i, 5)
                'below is the same as above but using join function.
                .Item(a(i, 1)) = Join(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4), a(i, 5)), ";")
            Else
                If Split(.Item(a(i, 1)), ";")(3) > a(i, 3) Then
                    .Item(a(i, 1)) = Join(Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4), a(i, 5)), ";")
                End If
            End If
        Next
        a = .items
     End With
     
     With Worksheets("output")
        For i = 0 To UBound(a)
            .Cells(i + 1, 1).Resize(, 5) = Split(a(i), ";")
        Next
     End With
    End Sub

  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
    If Split(.Item(a(i, 1)), ";")(3) > a(i, 3) Then
    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

    Split(.Item(a(i, 1)), ";")(2) > a(i, 3)
    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
    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?

  22. #22
    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.

  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
    Split(.Item(a(i, 1)), ";")(4) > a(i, 5)

  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