Find First Instance of Cell Value and Delete Corresponding Entire Row

David04Ruiz

New Member
Joined
Aug 29, 2022
Messages
18
Office Version
  1. 365
Platform
  1. 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:

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:
Screenshot 2022-12-07 200054.png


After:
Screenshot 2022-12-07 200140.png


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!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I'm assuming this is the result you want:

Before :
1670462976752.png

After:
1670463000739.png

I've changed the code a bit to get the result above.

VBA Code:
Set ws = Sheets("Sheet6")  ' sheet with data
col = 1  ' data column to check
Lrow = ws.Range("A" & Rows.Count).End(xlUp).Row
For I = Lrow To 2 Step -1 'Starts from Row 2
    If ws.Cells(I, col) = "" Then: Exit For  ' end of data
    If ws.Cells(I, col).Value = ws.Cells(I + 1, col).Value Then  ' if does not match previous row
        Rows(I).EntireRow.Delete  ' delete row
    End If

Next
 
Upvote 0
Try this :
VBA Code:
Set ws = Sheets("Sheet6")  ' sheet with data
col = 1  ' data column to check
Lrow = ws.Range("A" & Rows.Count).End(xlUp).Row
For I = Lrow To 2 Step -1 'Starts from Row 2
    If ws.Cells(I, col) = "" Then: Exit For  ' end of data
    If ws.Cells(I, col).Value <> ws.Cells(I - 1, col).Value Then  ' if does not match previous row
        Rows(I).EntireRow.Delete  ' delete row
    End If

Next
It'll give you this result :
1670463695224.png
 
Upvote 0
Solution
Try:
PHP:
Sub TEST()
Dim lr&, cell As Range, u As Range
With Sheets("Sheet6")  ' sheet with data
    lr = .Cells(Rows.Count, "A").End(xlUp).Row ' row of last data in column A
    For Each cell In .Range("A2:A" & lr)
        If WorksheetFunction.CountIf(.Range("A2", cell), cell) = 1 Then ' if cell was found 1st time
            If u Is Nothing Then
                Set u = cell
            Else
                Set u = Union(u, cell) ' combine cells those found 1st time
            End If
        End If
    Next
End With
u.EntireRow.Delete
End Sub
 
Upvote 0
Try this :
VBA Code:
Set ws = Sheets("Sheet6")  ' sheet with data
col = 1  ' data column to check
Lrow = ws.Range("A" & Rows.Count).End(xlUp).Row
For I = Lrow To 2 Step -1 'Starts from Row 2
    If ws.Cells(I, col) = "" Then: Exit For  ' end of data
    If ws.Cells(I, col).Value <> ws.Cells(I - 1, col).Value Then  ' if does not match previous row
        Rows(I).EntireRow.Delete  ' delete row
    End If

Next
It'll give you this result :
View attachment 80410
This code worked. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

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