How can I simplify this?

vba4PTV

New Member
Joined
May 5, 2023
Messages
6
Office Version
  1. 365
Hi,
New to vba and using it to make macros. I built a macro based off one I found online. It's designed to delete rows if they have certain criteria, but each criteria is in separate auto filter section. Is there a way to list all of the criteria(TV show names) in one "section" ?

Sub DeleteRowsF()
With ActiveSheet
.AutoFilterMode = False
With Range("f1", Range("f" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Big Bang Theory*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False


.AutoFilterMode = False
With Range("f1", Range("f" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Modern Family*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False


.AutoFilterMode = False
With Range("f1", Range("f" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Vanderpump Rules*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False


End With
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi @vba4PTV .
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.​

Try this:

VBA Code:
Sub DeleteRows()
  Dim arr As Variant, itm As Variant
  
  arr = Array("Big Bang Theory", "Modern Family", "Vanderpump Rules")
  For Each itm In arr
    Columns("F").Replace what:="*" & itm & "*", replacement:="#N/A", Lookat:=xlWhole
  Next
  On Error Resume Next
  Range("F1", Range("F" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Solution
Thank you for the warm welcome Dante! This worked perfect after I added a lot more names. In mine I used The asterisks around the show names to delete them when the name comes in a little different, like "Modern Family X-Mas Special" instead of just "Modern Family". Will this still work for those?
 
Upvote 0
"Modern Family X-Mas Special" instead of just "Modern Family". Will this still work for those?
Of course.
Works for "Big Band Theory 1", "Big Band Theory 2", "Big Band Theory 3", or "New Big Band Theory 1", etc.
The asterisks are in this part: what:="*" & itm & "*"

Such is this world of macros and programming :cool: . Everything must be tested and if something does not work we correct it. 😅
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

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