Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Count cells in column containing specified value

Tags:

excel

vba

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?

like image 820
BradStevenson Avatar asked Dec 21 '11 16:12

BradStevenson


People also ask

How do I count cells with values in Excel VBA?

The formula is =COUNTA(A2:A2000) : non-blank cells are counted. Show activity on this post. Show activity on this post.

How do I count cells in a column in VBA?

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.

How do I count a specific value in a column?

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.


2 Answers

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.

like image 192
JMax Avatar answered Oct 02 '22 13:10

JMax


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.

like image 21
Tezzums Avatar answered Oct 02 '22 12:10

Tezzums