I need to remove all records that don’t have 3 as the 2nd digit in the primary key field which for example can either look like this
Or without the #3
What I want is that all cells that have a non #3 start, their rows be deleted
I came up with the following code,
It removes some non #3, but not all.
I have over 13000 rows to scan
Sub keep3() ' ' RemoveNum Macro Dim i As Integer For i = 2 To 14000 If InStr(Cells(i, 2), "3") = 2 Then Else Rows(i).EntireRow.Delete End If Next i End Sub
If you can point me in the right direction, I’d be grateful!
When you delete a row all the rows below it move up, then you increment “i” which essentially skips the newly moved row. Try the following instead…
Sub keep3() ' ' RemoveNum Macro Dim i As Integer i = 2 While i < 14000 If InStr(Cells(i, 2), "3") = 2 Then ' Do nothing i = i + 1 Else Rows(i).EntireRow.Delete ' Note - not incrementing i here End If Wend End Sub
Remove the line that has the
Else. I think it is preventing the delete from working in the way you want. I am assuming that you do not have more than 14000 lines of data….
Or it might be that when you delete the row it messes up the order of the rows that you are running through.