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?
The formula is =COUNTA(A2:A2000) : non-blank cells are counted. Show activity on this post. Show activity on this post.
Both the Excel and VBA methods make use of the COUNTA function and selecting an entire column to count cells from a single column that contain a value.
To count cells that equal a specific value, use the COUNTIF function. The COUNTIF function counts the number of cells in the specified range which meet the specified criteria. To give the condition simply type the value or provide the cell reference.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With