Combining multiple macros into 1?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
How can I do this more efficiently? Currently trying to cleanup a bunch of macros I've gathered through this sub and the internet. Wondering if cleaning it up further is possible. I have these subs which are essentially the same with the only difference being the .AutoFilter 1 "*Value*" Is there a way of doing the same thing with one sub?
VBA Code:
Sub wyoming()
With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*wyoming*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub

Sub wisconsin()
With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*wisconsin*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub

Sub washington()
With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*washington*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub

Sub virginia()
With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*virginia"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You might consider the following...

VBA Code:
Sub States()
Dim States(4) As Variant
Dim i As Long

States(1) = "wyoming"
States(2) = "wisconsin"
States(3) = "washington"
States(4) = "virginia"

With ActiveSheet
    For i = 1 To 4
        .AutoFilterMode = False
        With Range("d1", Range("d" & Rows.Count).End(xlUp))
            .AutoFilter 1, "*" & States(i) & "*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    Next i
End With
End Sub

Cheers,

Tony
 
Upvote 0
Solution
You might consider the following...

VBA Code:
Sub States()
Dim States(4) As Variant
Dim i As Long

States(1) = "wyoming"
States(2) = "wisconsin"
States(3) = "washington"
States(4) = "virginia"

With ActiveSheet
    For i = 1 To 4
        .AutoFilterMode = False
        With Range("d1", Range("d" & Rows.Count).End(xlUp))
            .AutoFilter 1, "*" & States(i) & "*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    Next i
End With
End Sub

Cheers,

Tony
Thank you so much! I'm still amazed as to how knowledgeable you guys are in this sub. You guys have truly made our jobs easier by helping us automate a lot of the repetitive stuff.
 
Upvote 0
@tonyyy does the "12" in this line: Offset(1).SpecialCells(12).EntireRow.Delete means there are 12 columns?
 
Upvote 0
"Thank you so much!"
You're very welcome.

"does the "12" in this line: Offset(1).SpecialCells(12).EntireRow.Delete means there are 12 columns?"
The number 12 is an XLCellType enumeration. It's a shorthand way of specifying xlCellTypeVisible.
 
Upvote 0
"Thank you so much!"
You're very welcome.

"does the "12" in this line: Offset(1).SpecialCells(12).EntireRow.Delete means there are 12 columns?"
The number 12 is an XLCellType enumeration. It's a shorthand way of specifying xlCellTypeVisible.
Thank you for the link! I'm one of those that does not really understand the basics, still fascinated by macros though.
 
Upvote 0

Forum statistics

Threads
1,214,866
Messages
6,121,996
Members
449,060
Latest member
mtsheetz

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