How to combine 2 macros deleting rows with certain criteria

cwills

New Member
Joined
Aug 6, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi

I currently have 2 macros set up to search a sheet for different words and delete the row if that word is present.

I would like to combine these into a single macro which would check for both words and delete the row if either of them are present

The words are in different columns
I search for the word "resident" in column G with this

' Delete Any resident stores
Dim ws As Worksheet
Dim strSearch As String
Dim lRow As Long

strSearch = "Resident"

Set ws = Sheets("Site data")
With ws
lRow = .Range("G" & .Rows.Count).End(xlUp).Row
'~~> Remove any filters
.AutoFilterMode = False
'~~> Filter, offset(to exclude headers) and delete visible rows
With .Range("G1:G" & lRow)
.AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

'~~> Remove any filters
.AutoFilterMode = False
End With

And I then search for the word "Limited" in Column H using this


Dim ws As Worksheet
Dim strSearch As String
Dim lRow As Long

strSearch = "Limited"
Set ws = Sheets("Site data")
With ws
lRow = .Range("H" & .Rows.Count).End(xlUp).Row
'~~> Remove any filters
.AutoFilterMode = False
'~~> Filter, offset(to exclude headers) and delete visible rows
With .Range("H1:H" & lRow)
.AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With

'~~> Remove any filters
.AutoFilterMode = False
End With

What's the best way to combine these 2 into a single macro to search for both?

Thank you
Colin
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I would probably add the code as a separate Sub that you can call with given parameters. That way you can add another call if needed or steal it and take it to another macro later.

You can create an advanced filter to filter 2 columns at the same time, but you have to have the criteria in a range (unless I am mistaken). The macro could write this range itself and then delete it or the filter criteria could already be on a sheet somewhere, but to me it is easier to just call the RemoveRows sub twice like below.

VBA Code:
Sub ProcessRows()

    Dim ws As Worksheet
     
    Set ws = Sheets("Site data")

    Call RemoveRows(ws, "Resident", "G")
    Call RemoveRows(ws, "Limited", "H")

End Sub

Sub RemoveRows(ws As Worksheet, strSearch As String, myCol As String)

    Dim lRow As Long
 
    With ws
        lRow = .Range(myCol & .Rows.Count).End(xlUp).Row
        '~~> Remove any filters
        .AutoFilterMode = False
        '~~> Filter, offset(to exclude headers) and delete visible rows
        With .Range(myCol & "1:" & myCol & lRow)
            .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With
        
        '~~> Remove any filters
        .AutoFilterMode = False
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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