How to delete a row if its values are equal to another?

mtbthepro

Board Regular
Joined
Feb 22, 2017
Messages
91
Hello,
I have this code which should read all rows that begin with the word ACCOUNT#: which is followed by numbers like this: 1-026-31-00000-3342. The whole this looks like this ACCOUNT#: 1-026-31-00000-3342. If the code reads some row and there is another row identical to the row read in the beginning then the second row needs to get deleted.
Is it possible? This is what I have making so far but it doesn't work because the match function doesn't read that value.
Code:
Sub SECOND_MACRO_Delete_DuplicateAccount()
    
    Dim ws As Worksheet, row1 As Long, row2 As Long, rng As Long, n As Long, nlast As Long, rw As Range
    Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows
    nlast = rw.Count
    
    For Each ws In Sheets
                    For r = 1 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
                        If ws.Cells(r, "A") Like "*ACCOUNT*" Then
                    row1 = WorksheetFunction.Match(ws.Cells(r, "A").Value, ws.Columns(1), 0)
                    row2 = WorksheetFunction.Match(ws.Cells(r, "A").Value, ws.Columns(1), 0)
                        If ws.Range("A" & row1).Value = ws.Range("A" & row2).Value Then
                        ws.Rows(row2).Delete
                        
                        End If
                    End If
            Next ws
          End Sub

 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Maybe this macro:

Code:
Sub remove()
Dim a As Long
For a = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Range("A1:A" & a), Cells(a, 1)) > 1 Then Rows(a).Delete
Next
End Sub
 
Last edited by a moderator:
Upvote 0
Maybe this macro:

Code:
Sub remove()
Dim a As Long
For a = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
If WorksheetFunction.CountIf(Range("A1:A" & a), Cells(a, 1)) > 1 Then Rows(a).Delete
Next
End Sub
Hi there, that deletes all rows at are equal to eachother but is there a way to only delete rows if they contain ACCOUNT# and have matching numbers.
 
Last edited:
Upvote 0
Use the concatenate to combine the columns first into column A. Then run the code
 
Upvote 0
Maybe it would be better to make it a table and sort the data however you wish.
 
Upvote 0
You might consider the modified code below...

Code:
Sub remove()
Dim a As Long
For a = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If WorksheetFunction.CountIf(Range("A1:A" & a), Cells(a, 1)) > 1 _
        And Left(Cells(a, 1), 8) = "ACCOUNT#" Then Rows(a).Delete
Next
End Sub

This will delete matching rows that start with "ACCOUNT#" and ignore all others.

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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