David04Ruiz
New Member
- Joined
- Aug 29, 2022
- Messages
- 18
- Office Version
- 365
- Platform
- Windows
Hi all, I am trying to get a code that goes down column A (starting with row 2) and looks for the first instance that a cell value appears. Upon detecting that, it will delete that entire row. I am using the following code I found from another user:
Before:
After:
As you can see, it has an issue when the "first instance" is the only instance. The result should have deleted the first "D" value and the first "F" value. The user that I got the code from only had duplicated values, not a combination of duplicates and unique like me. Would anyone happen to know how to fix this or have an alternative I could try? Thank you in advance!
VBA Code:
Set ws = Sheets("Sheet6") ' sheet with data
col = 1 ' data column to check
rw = 2 ' data start row
For ctr = 1 To 9999 ' prevent endless loop
If ws.Cells(rw, col) = "" Then: Exit For ' end of data
If ws.Cells(rw, col).Value <> ws.Cells(rw - 1, col).Value Then ' if does not match previous row
Rows(rw).EntireRow.Delete ' delete row
End If
rw = rw + 1 ' next row
Next
Before:
After:
As you can see, it has an issue when the "first instance" is the only instance. The result should have deleted the first "D" value and the first "F" value. The user that I got the code from only had duplicated values, not a combination of duplicates and unique like me. Would anyone happen to know how to fix this or have an alternative I could try? Thank you in advance!