Macro to remove entire row if text in column A isn't contained in a range

AV_Geek

New Member
Joined
Jan 23, 2022
Messages
32
Office Version
  1. 365
Platform
  1. MacOS
I'm looking for a macro that will delete the entire is none of the text is contained in a list.

Example:

Sheet1

ABCD
1Alpha One111422
2Beta Two1912185
3Charlie Three419211
4Delta Four172615
2. 8. 16. 22


Sheet 2

ABCD
1Beta
2Three

I'm looking to delete Rows 2 and 3 from Sheet 1 based off of the criteria in Sheet2 Range A1:A2
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi, try this..
VBA Code:
Sub test()
    Dim v, values, lR&, i&
    With Sheets("Sheet2")
        values = .Range("A1:A" & .Cells(Rows.Count, 1).End(3).Row).Value
    End With
    With Sheets("Sheet1")
        lR = .Cells(Rows.Count, 1).End(3).Row
        For Each v In values
            For i = 1 To lR
                If .Cells(i, 1).Value <> "" And InStr(.Cells(i, 1).Value, v) Then
                    .Cells(i, 1).ClearContents
                End If
            Next i
        Next v
        With .Range("A1:A" & lR)
            If WorksheetFunction.CountBlank(.Cells) > 0 Then
                .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            End If
        End With
    End With
End Sub
 
Upvote 0
Nice!!!!!!! One more thing - can we get it to keep the row when column A is blank??
 
Upvote 0
Nice!!!!!!! One more thing - can we get it to keep the row when column A is blank??
VBA Code:
Sub test()
    Dim v, values, lR&, i&, rng As Range
    With Sheets("Sheet2")
        values = .Range("A1:A" & .Cells(Rows.Count, 1).End(3).Row).Value
    End With
    With Sheets("Sheet1")
        lR = .Cells(Rows.Count, 1).End(3).Row
        For Each v In values
            For i = 1 To lR
                If .Cells(i, 1).Value <> "" And InStr(.Cells(i, 1).Value, v) Then
                    If rng Is Nothing Then
                        Set rng = .Cells(i, 1)
                    Else
                        Set rng = Union(rng, .Cells(i, 1))
                    End If
                End If
            Next i
        Next v
        If Not rng Is Nothing Then rng.EntireRow.Delete
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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