Using wildcard in vba countif function

carl.hayes

New Member
Joined
Oct 16, 2008
Messages
21
I am trying to return how many cells contain a string of text as entered by the user but I am struggling to put wildcards around my search term. Below is the code I have so far.

Can anyone help me with this?

Code:
Dim search as String
Dim occurance As Integer

search = InputBox("Search for", "Search Term")
occurance = WorksheetFunction.CountIf(Cells, search)

MsgBox (occurance)
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try

Code:
Dim search As String
Dim occurance As Integer

search = InputBox("Search for", "Search Term")
occurance = WorksheetFunction.CountIf(Cells, "*" & search & "*")

MsgBox occurance
 
Upvote 0
Thanks VoG,
It seems like I was almost there but had missed the spaces either side of the & which meant it didn't work.

You have saved me from going totally crazy trying to figure this out.
 
Upvote 0

Forum statistics

Threads
1,224,844
Messages
6,181,294
Members
453,030
Latest member
PG626

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