Delete rows based on 3 criteria

Jak

Well-known Member
Joined
Apr 5, 2002
Messages
833
Hi, I am looking for a VBA solution to automate the removal of unwanted data. The number of records varies.

In the image below, I have highlighted 4 rows I wish to keep. This is a small representation of the data.
1659095405948.png


Each ID number has a REV number and a State. I need to keep the lowest Rev number row for each ID where the State is either Committed or Done. So when the unwanted data is removed, I would end up with a header row and the 4 rows in this example.

1659095740245.png


Any help with this would be appreciated.
 

Attachments

  • 1659094399737.png
    1659094399737.png
    55.1 KB · Views: 3

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If the data is sorted by ID and Rev number, then try the following code on a copy of your sheet.

VBA Code:
Sub deleterows()
  Dim rng As Range
  Dim i As Long, lr As Long
  Dim dic As Object
  Dim cad As String
  
  lr = Range("A" & Rows.Count).End(3).Row
  Set rng = Range("A" & lr + 1)
  Set dic = CreateObject("Scripting.Dictionary")
  
  For i = 2 To lr
    If Range("G" & i).Value = "Committed" Or Range("G" & i).Value = "Done" Then
      cad = Range("A" & i).Value & "|" & Range("G" & i).Value
      If Not dic.exists(cad) Then
        dic(cad) = Empty
      Else
        Set rng = Union(rng, Range("A" & i))
      End If
    Else
        Set rng = Union(rng, Range("A" & i))
    End If
  Next
  rng.EntireRow.Delete
End Sub

---------------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Solution
If the data is sorted by ID and Rev number, then try the following code on a copy of your sheet.

VBA Code:
Sub deleterows()
  Dim rng As Range
  Dim i As Long, lr As Long
  Dim dic As Object
  Dim cad As String
 
  lr = Range("A" & Rows.Count).End(3).Row
  Set rng = Range("A" & lr + 1)
  Set dic = CreateObject("Scripting.Dictionary")
 
  For i = 2 To lr
    If Range("G" & i).Value = "Committed" Or Range("G" & i).Value = "Done" Then
      cad = Range("A" & i).Value & "|" & Range("G" & i).Value
      If Not dic.exists(cad) Then
        dic(cad) = Empty
      Else
        Set rng = Union(rng, Range("A" & i))
      End If
    Else
        Set rng = Union(rng, Range("A" & i))
    End If
  Next
  rng.EntireRow.Delete
End Sub

---------------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Thanks for the quick reply and code, works a treat. Thanks muchly for the tip on XL2BB
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top