Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Countif With Multiple OR Criteria

I need to count the number of cells in a column which contain a specific value. I am currently using the following code:

iVal = Application.WorksheetFunction.COUNTIF(Range("A:A"), "SAL")

However, I now need to count how many cells contain either

  • SAL, or
  • PRE
like image 1000
BradStevenson Avatar asked Jan 04 '12 12:01

BradStevenson


People also ask

Can I use Countif with multiple criteria?

Excel has many functions where a user needs to specify a single or multiple criteria to get the result. For example, if you want to count cells based on multiple criteria, you can use the COUNTIF or COUNTIFS functions in Excel.

Can you use Countif for multiple columns?

Using COUNTIFS along with Current Date Functions to Count Cells across Multiple Columns. We can also insert TODAY function while working with COUNTIFS formula.


3 Answers

You could use the VBA equivalent of a standard COUNTIF formula (which can take more than one argument). This can be expanded as required

Standard Formula
=SUM(COUNTIF(A:A, {"SAL","PRE"}))

VBA Equivalent
MsgBox Evaluate("Sum(COUNTIF(A:A,{""PRE"",""SAL""}))")

like image 109
brettdj Avatar answered Nov 08 '22 23:11

brettdj


The quickest way would just be to do 2 COUNTIFs:

iVal = Application.WorksheetFunction.CountIf(Range("A:A"), "SAL") + Application.WorksheetFunction.CountIf(Range("A:A"), "PRE")
like image 37
Jonathan Sayce Avatar answered Nov 09 '22 00:11

Jonathan Sayce


Why dont you use the CountIfs function?

Example:

Application.CountIfs(CR1_range, V_1, CR2_range, V_2, CR3_range, V_3, CR4_range, V_4)

Where CR1_range is obviously your range and V_1 is your variable

Edit: Ooops! Just seen that it must be a OR statement, my answer would only work for an AND statement (didn't delete for reference incase anyone is interested)

like image 23
LBPLC Avatar answered Nov 08 '22 23:11

LBPLC