Using Countif with wildcard to delete rows

DavidSCowan

Board Regular
Joined
Jun 7, 2009
Messages
78
Hi there,
I am a VBA novice and I would greatly appreciate some help.
I am using Countif in a relatively simple macro to identify if a cell contains a particular string. The idea is to delete the row if the string is present.
So for example the two lines of code would read:
If Application.WorksheetFunction.CountIf(Cells(j, 1), "*Aerosol*") = 1 Then
Cells(j, 1).EntireRow.Delete
i.e. delete the row j if Cells(j,1) contains the word “Aerosol”
This works but I would like to replace Aerosol with Cells(i,2) so that I can cycle through a column of words in Column 2 and delete the row if the relevant words are present.
But what do I replace “*Aerosol*” with?
I have tried "*Cells(i,2)*" and "*Cells(i,2).value*" but with both when I F8 through the macro the looping works but rows that should be deleted aren’t i.e. the result is that no rows are deleted
When I replace "*Aerosol*" with *Cells(i,2).value* (i.e. no “ “) this produces an error (Compile Error Expected expression)
What should I replace "*Aerosol*" with?
Thank you in advance
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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