deleting rows with macro

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
531
Office Version
  1. 365
Platform
  1. Windows
hi!
how can i delete rows with multiple cells with specific values ?
example: i want to delete the rows (entirely) which contain only the numbers 1 and 4 and 9 ?


22.xlsx
ABCDE
151149
211159
342419
412129
512489
613519
712459
882469
922479
22
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Not sure I understand exactly what you want. Assuming you want to delete entire rows containing 1 and 4 and 9, try this.
VBA Code:
Sub RemoveRowsIf()
'removes entire rows containing Num1 and Num2 and Num3
'assumes data start in cell A1
Dim R As Range, V As Variant, i As Long, j As Long, Ct As Long, d As Object
Const Num1 As Long = 1: Const Num2 As Long = 4: Const Num3 As Long = 9   ' change Nums to suit
Set R = Range("A1").CurrentRegion
Set d = CreateObject("Scripting.dictionary")
V = R.Value
    For i = 1 To UBound(V, 1)
        For j = 1 To UBound(V, 2)
            If V(i, j) = Num1 Or V(i, j) = Num2 Or V(i, j) = Num3 Then
                If Not d.exists(V(i, j)) Then
                    d.Add V(i, j), ""
                    Ct = Ct + 1
                        If Ct = 3 Then
                            V(i, j) = "#N/A"
                            Ct = 0
                            d.RemoveAll
                            GoTo Nx
                        End If
                End If
            End If
        Next j
Nx:
    Next i
R.Value = V
On Error Resume Next
R.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub
 
Upvote 0
Try this too
VBA Code:
Sub Delete_Rows()
    Dim DelRow As Range
    For Each DelRow In Range("A1:A9")
       If Not IsError(Application.Match(1, Rows(DelRow.Row), 0)) And _
            Not IsError(Application.Match(4, Rows(DelRow.Row), 0)) And _
            Not IsError(Application.Match(9, Rows(DelRow.Row), 0)) Then
            DelRow.EntireRow.Delete
        End If
    Next
End Sub
 
Upvote 0
Try this too
VBA Code:
Sub Delete_Rows()
    Dim DelRow As Range
    For Each DelRow In Range("A1:A9")
       If Not IsError(Application.Match(1, Rows(DelRow.Row), 0)) And _
            Not IsError(Application.Match(4, Rows(DelRow.Row), 0)) And _
            Not IsError(Application.Match(9, Rows(DelRow.Row), 0)) Then
            DelRow.EntireRow.Delete
        End If
    Next
End Sub
That will not work if you have consecutive rows that need deleting.

When deleting rows, you either:
- Need to wait and delete them all at once like JoeMo did.
or
- If deleting with each loop, you need to loop through your range backwards, so as not to miss any rows.
 
Upvote 0
Thanks to excelNewbie22 for testing the code I posted in Post #2 and noting that for some of his data it doesn't produce the expected results. The problem was with the placement of two lines. I have corrected that below.
VBA Code:
Sub RemoveRowsIf()
'removes entire rows containing Num1 and Num2 and Num3
'assumes data start in cell A1
Dim R As Range, V As Variant, i As Long, j As Long, Ct As Long, d As Object
Const Num1 As Long = 1: Const Num2 As Long = 5: Const Num3 As Long = 7   ' change Nums to suit
Set R = Range("A1").CurrentRegion
Set d = CreateObject("Scripting.dictionary")
V = R.Value
    For i = 1 To UBound(V, 1)
        For j = 1 To UBound(V, 2)
            If V(i, j) = Num1 Or V(i, j) = Num2 Or V(i, j) = Num3 Then
                If Not d.exists(V(i, j)) Then
                    d.Add V(i, j), ""
                    Ct = Ct + 1
                        If Ct = 3 Then
                            V(i, j) = "#N/A"
                            GoTo Nx
                        End If
                End If
            End If
        Next j
Nx: d.RemoveAll
    Ct = 0
    Next i
R.Value = V
On Error Resume Next
R.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,958
Messages
6,175,638
Members
452,663
Latest member
MEMEH

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