38

I need to write a macro that searches a specified column and counts all the cells that contain a specified string, such as "19/12/11" or "Green" then associate this number with a variable,

Does anyone have any ideas?

5 Answers 5

67

Do you mean you want to use a formula in VBA? Something like:

Dim iVal As Integer
iVal = Application.WorksheetFunction.COUNTIF(Range("A1:A10"),"Green")

should work.

1
  • 7
    By the way, you can add wildcards to your search string like this: iVal = Application.WorksheetFunction.COUNTIF(Range("A1:A10"),"*Green*")
    – Kes Perron
    Commented Oct 30, 2015 at 15:57
15

This isn't exactly what you are looking for but here is how I've approached this problem in the past;

You can enter a formula like;

=COUNTIF(A1:A10,"Green")

...into a cell. This will count the Number of cells between A1 and A10 that contain the text "Green". You can then select this cell value in a VBA Macro and assign it to a variable as normal.

2
  • Your answer desserves a +1 for giving the right way to follow. And I hope this will encourage you to keep on answering other questions
    – JMax
    Commented Dec 24, 2011 at 14:28
  • Good answer thank you, shown a different way I can go about it Commented Jan 4, 2012 at 12:14
2

one way;

var = count("find me", Range("A1:A100"))

function count(find as string, lookin as range) As Long
   dim cell As Range
   for each cell in lookin
       if (cell.Value = find) then count = count + 1 '//case sens
   next
end function
2
  • Works - but will be very slow on decent sized ranges to test cell by cell, better to use a function like JMax did, or use the range Find method
    – brettdj
    Commented Dec 22, 2011 at 6:09
  • Works, and is great thanks, but as above JMax's method seems to work better for me Commented Jan 4, 2012 at 12:13
1

If you're looking to match non-blank values or empty cells and having difficulty with wildcard character, I found the solution below from here.

Dim n as Integer
n = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
0

Not what you asked but may be useful nevertheless.

Of course you can do the same thing with matrix formulas. Just read the result of the cell that contains:

Cell A1="Text to search"
Cells A2:C20=Range to search for

=COUNT(SEARCH(A1;A2:C20;1))

Remember that entering matrix formulas needs CTRL+SHIFT+ENTER, not just ENTER. After, it should look like :

{=COUNT(SEARCH(A1;A2:C20;1))}

2
  • 2
    bummi ... Wouldn't it be cool if someone that does a negative evaluation tells why ? So that next time, I wont make the same mistake ? I know I'm a newbie, so I could be wrong ... Commented Dec 19, 2014 at 13:56
  • 1
    I'm not the person who voted this down, but my guess is that the question started out "I need to write a macro" and it had "VBA" at the beginning of the title, but your answer is actually a worksheet function. I, too, find it annoying to see downvotes with no explanation.
    – Alex R.
    Commented May 31, 2017 at 21:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.