Multiple remove in VBA

emlou187

New Member
Joined
Mar 21, 2018
Messages
5
Hi,

I am trying to run something that will remove the entire row if a series of letters are present.

I am using the below code. It works, but I need to add in around another 25 letter combinations (i.e at the moment it removes EZY, but I also need to remove EX, EFT etc etc.)

Can anyone help??

Sub DeleteRows()
Dim c As Range
Dim SrchRng


Set SrchRng = ActiveSheet.Range("B1", ActiveSheet.Range("B65536").End(xlUp))
Do
Set c = SrchRng.Find("EZY", LookIn:=xlValues)

If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Re: Struggling - multiple remove in VBA

If you are looking for exact matches try
Code:
Sub FilterDel()
   Dim Ary As Variant
   
   Ary = Array("EZY", "EX", "EFT")
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      .Range("H:H").AutoFilter 1, Ary, xlFilterValues
      .UsedRange.Offset(1).SpecialCells(xlVisible).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Re: Struggling - multiple remove in VBA

It is not exact, it just contains it. So there will be EZY123, EZY452, EZY634 etc etc and I need all those gone. :-(
 
Upvote 0
Re: Struggling - multiple remove in VBA

In that case, try
Code:
Sub FilterDel()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("*EZY*", "*EX*", "*EFT*")
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      For i = LBound(Ary) To UBound(Ary)
         .Range("B:B").AutoFilter 1, Ary(i)
         .UsedRange.Offset(1).SpecialCells(xlVisible).EntireRow.Delete
      Next i
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Re: Struggling - multiple remove in VBA

No the data still isn't going anywhere ;-(

It's doing my head in, can't figure it out at all.
 
Upvote 0
Re: Struggling - multiple remove in VBA

If you step through the code using F8, does the data set get filtered? & are there any visible rows?
 
Upvote 0
Re: Struggling - multiple remove in VBA

It works if I keep it to 5 different codes to remove or less. The second I put more letter combinations in it just stops working. I have 32 different combos..........
 
Upvote 0
Re: Struggling - multiple remove in VBA

It should work regardless of how many values you have in the array.
How have you added them?
 
Upvote 0
Re: Struggling - multiple remove in VBA

It should work regardless of how many values you have in the array.
How have you added them?

Adding them manually, typing them in. Doesn't matter which order I put them in the first 5 will work and then not after that. :confused:
 
Upvote 0
Re: Struggling - multiple remove in VBA

Hello Emlou187,

Here's another way that may work.

Place the 32 combinations in Column A of, say, Sheet2 starting in cell A1. Now apply the following code:-

Code:
Sub DeleteIt()

Dim ar As Variant
ar = Sheet2.Range("A1", Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp))

For i = 1 To UBound(ar)
    With Sheet1.[A1].CurrentRegion
        .AutoFilter 2, ar(i, 1), , , 7
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
Next i

End Sub

As you can see, I'm referencing the sheets by their sheet codes (Sheet1 and Sheet2). You could change Sheet1 to ActiveSheet in the above code.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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