+ Reply to Thread
Results 1 to 2 of 2

Macro required to delete rows based on date

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2008
    Location
    SINGAPORE
    Posts
    1

    Macro required to delete rows based on date

    Hi

    I'm completely new to VBA in Excel , appreciate that I need to do some learning but I need a quick solution to this particular problem - if anyone can help would be really appreciated

    I have a large spreadsheet with date stamped data where each row has been created. Date in column 'R'. I need the last created row for a particular object (i.e. all other earlier rows need to be deleted). Each object has a unique identifier in column 'D'

    So essentially based on the date in column R, I need to delete all rows except the latest one created for each unique object

    Many thanks!

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    See how this goes.
    Sub aaa()
      Dim nodupes As New Collection
      For Each ce In Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
        On Error Resume Next
        nodupes.Add Item:=ce.Value, key:=CStr(ce.Value)
        On Error GoTo 0
      Next ce
    
      For i = 1 To nodupes.Count
        lastrow = Cells(Rows.Count, "D").End(xlUp).Row
        maxdate = Evaluate("=MAX(IF(D2:D" & lastrow & "=" & nodupes(i) & ",R2:R" & lastrow & ",""""))")
        For j = lastrow To 2 Step -1
          If Cells(j, "D") = nodupes(i) And Cells(j, "R") <> maxdate Then
            Cells(j, "D").EntireRow.Delete
          End If
        Next j
        
      Next i
    End Sub
    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Delete first 7 rows out of 21 and so on.
    By ducati in forum Excel General
    Replies: 7
    Last Post: 09-12-2008, 12:36 PM
  2. Macro to delete rows if less than above
    By pghall1000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2008, 11:30 AM
  3. Macro that fills in cells needs to skip 2 rows
    By westonkw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-03-2008, 05:36 PM
  4. Macro to delete alternative rows
    By claytorm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-29-2007, 07:59 AM
  5. macro to delete last 5 rows
    By JulianActon in forum Excel General
    Replies: 2
    Last Post: 09-13-2006, 05:32 PM

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