VBA - Find multiple strings in sheet and delete row

d3kk3r

New Member
Joined
Aug 19, 2022
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hi everyone,

I'm new to excel VBA and would like some help with a specific issue.

I have a worksheet with variable columns and rows.

I need to find multiple strings in each cell of the sheet and delete the entire row if any of the values are found

I was trying to adapt something like this:

VBA Code:
Sub deleteRow()
    Dim MyValue As String
    Dim strValueToRemove As Variant
    
    strValueToRemove = Array("Value1", "Value2", "Value3", "Value4")

    For Each cell In ActiveSheet.UsedRange.Cells
    
        MyValue = CStr(cell.Value)
        For a = 1 To 6
            If InStr(1, MyValue, strValueToRemove(a), vbTextCompare) > 0 Then
                cell.EntireRow.Delete
                Exit For
            End If
        Next
        
    Next cell

End Sub

But don't know how.

Can anyone help?

Thanks!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Please try the following on a copy of your data. Adjust the sheet name and values searched for to suit.

VBA Code:
Option Explicit
Sub d3kk3r()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ change sheet as appropriate
    
    Dim lRow As Long, lCol As Long
    lRow = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    lCol = ws.Cells.Find("*", , xlFormulas, , 2, 2).Column
    
    Dim rng As Range
    Set rng = ws.Range(ws.Cells(2, 1), ws.Cells(lRow, lCol))
    
    Dim a, b, vals
    a = rng.Value
    ReDim b(1 To UBound(a), 1 To 1)
    
    vals = Array("Value1", "Value2", "Value3", "Value4")    '<~~ change to the actual values searching for
    Dim i As Long, j As Long, k As Long
    For i = LBound(a, 1) To UBound(a, 1)
        For j = LBound(a, 2) To UBound(a, 2)
            For k = LBound(vals) To UBound(vals)
                If InStr(1, a(i, j), vals(k), 0) > 0 Then b(i, 1) = 1
            Next k
        Next j
    Next i
    
    lCol = lCol + 1
    ws.Cells(2, lCol).Resize(lRow - 1).Value = b
    i = WorksheetFunction.Sum(Columns(lCol))
    Range(Cells(2, 1), Cells(lRow, lCol)).Sort Key1:=Cells(2, lCol), order1:=1, Header:=2
    If i > 0 Then Cells(2, lCol).Resize(i).EntireRow.Delete
    
    Application.ScreenUpdating = True
    Application.Calculation = xlAutomatic
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,545
Messages
6,125,450
Members
449,227
Latest member
Gina V

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