hello guys!
how are you?
In excel, how can we use COUNT.IF function with multiple conditions/CRITERIA?
for example:
11 26 47 55 57 59 62 68 71 76 78 81 83 94 99
I wanna calculate with COUNT.IF function how many numbers fill up the following ALTERNATIVE CRITERIA:
COUNT.IF OF [ (A1:A15); "=11" OR ">=55 AND <=71" OR ">=83")]
TRANSLATING: how many numbers in the list above, are equal to 11 OR are greaten than/equal to 55 and lower than/equal to 71 OR are greaten than/equal to 83.?
Writen results are: 11, 55, 57, 59, 62 , 68, 71, 83, 94, 99
Thus COUNT.IF function should display the answer = 10 numbers.
It'd be useful to have a COUNT.IF function that do all the calculation with multiple conditions/CRITERIA.
I was wondering thatīs possible do it in excel/vba but I don't know how to start.
Can someone give a hand??
I'm looking forward to receiving good news.
6 1956
This macro will do it. - Sub Macro1()
-
Dim COUNTER As Integer
-
Range("A1").Activate
-
Do While ActiveCell.Value <> ""
-
With ActiveCell
-
If .Value = 11 Or _
-
(.Value >= 55 And .Value <= 71) Or _
-
.Value >= 83 Then _
-
COUNTER = COUNTER + 1
-
.Offset(0, 1).Activate
-
End With
-
Loop
-
Range("A2").Value = COUNTER
-
End Sub
Iīm so gratefull for your aid.
It really works.
Iīd like to do this, to make this all calculation for each line for a extremely large number list in a spreadsheet like excel.
for example:
line 01: 90 95 30 40 98 65 67 10 20 51
line 02: 55 68 35 69 75 77 79 80 84 85
and so on... for line 03 , line 04, line 5000, ... .
The spreadsheet should display the total result, as you did above, and the explicit writen results for each line in the number list.
As was done in the previous post:
Writen results are: 11, 55, 57, 59, 62 , 68, 71, 83, 94, 99
Thus COUNT.IF function should display the answer = 10 numbers.
Can you give some advice??
Thanks in advance
Hi
You can create your own user define function, Say ValuesCount()
Which could look something like this - Function ValuesCount(ByRef ThisRange As Range) As Integer
-
Dim cel As Range
-
-
For Each cel In ThisRange
-
If IsNumeric(cel) Then
-
Select Case True
-
Case cel.Value = 11
-
ValuesCount = ValuesCount + 1
-
Case cel.Value >= 83
-
ValuesCount = ValuesCount + 1
-
Case (cel.Value >= 55 And cel.Value <= 75)
-
ValuesCount = ValuesCount + 1
-
End Select
-
End If
-
Next cel
-
End Function
This should be pasted in a Code Module (not the Sheet or ThisWorkbook Module).
And used in the requied cell like this
=ValuesCount(A1:O1)
for example.
You can use this anywhere in the work book and specify any range you like.
HTH
MTB
If you have a sheet with more then 5000 lines, it's better to put the sheet in an array (much, much faster).
The code of the macro (not a function) will be: - Sub COUNT()
-
Dim ARRAYDATA As Variant '§ with range=>array must ALLWAYS be variant.
-
Dim ARRAYCOUNTER() As Integer '§ count results for each line
-
Dim ROW As Integer
-
Dim COL As Integer
-
Dim COUNTER As Integer
-
Dim TOTAL As Integer
-
'§ put sheet1 in array
-
Sheets(1).Select
-
'§ ( |------rows--------------|,|-----cols----------------------| )
-
ARRAYDATA = Range("A1").Resize(Range("A1").End(xlDown).ROW, Range("A1").End(xlToRight).Column)
-
'§ count each row
-
ReDim ARRAYCOUNTER(1 To UBound(ARRAYDATA, 1), 1 To 1)
-
For ROW = 1 To UBound(ARRAYDATA, 1)
-
COUNTER = 0
-
For COL = 1 To UBound(ARRAYDATA, 2)
-
If ARRAYDATA(ROW, COL) = 11 Or _
-
(ARRAYDATA(ROW, COL) >= 55 And ARRAYDATA(ROW, COL) <= 71) Or _
-
ARRAYDATA(ROW, COL) >= 83 Then _
-
COUNTER = COUNTER + 1
-
Next
-
ARRAYCOUNTER(ROW, 1) = COUNTER
-
Next
-
'§ copy ARRAYCOUNTER to sheet
-
Range("L1").Resize(UBound(ARRAYCOUNTER), 1) = ARRAYCOUNTER
-
'§ calculate TOTAL
-
TOTAL = 0
-
For ROW = 1 To UBound(ARRAYCOUNTER, 1)
-
TOTAL = TOTAL + ARRAYCOUNTER(ROW, 1)
-
Next
-
'§ put TOTAL in sheet
-
Range("L" & CStr((UBound(ARRAYCOUNTER, 1) + 2))).Value = TOTAL
-
End Sub
@Guido Geurs
Many thanks for your help.
It really works.
The spreadsheet should display the total result for each line, as you did above, but it didnīt display the explicit writen results for each line in the number list.
for example:
11 26 47 55 57 59 62 68 71 76 78 81 83 94 99
Thus COUNT.IF function should display the writen results at the right side of the spreadsheet : 11, 55, 57, 59, 62 , 68, 71, 83, 94, 99
Shall you fix it up?
Thanks in advance
this will add the explicit numbers to the sheet: see attachment
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paxton |
last post by:
Hi,
I'm trying to display the total page views per page within a given date
range, but the correct SQL is seemingly beyond me. I get the correct result
with a straightforward Group By and Count...
|
by: Steve Jorgensen |
last post by:
Mailing List management is a good example of a case where my conundrum arises.
Say there is a m-m relationship between parties and groups - anyone can be a
member of any combintation of groups. ...
|
by: dBNovice |
last post by:
Hey group,
I am trying to do a count of the number of papers in a table. The
table has a PaperID that differentiates each paper , e.g. 004.1. Some
papers are reused. The reused paper is given...
|
by: DrewKull |
last post by:
Ok ... so I'm looking at a query where im trying to show a bunch of
columns counted up based on criteria ...
So far i've been able to count all the rows up and show them ... and
also count up...
|
by: technocraze |
last post by:
Hi community experts,
I am having an isue with Dsum function that is used to count the total number for a particular field (intake) at the textbox afterupdate event with condition/ criteria...
|
by: KCangelstar |
last post by:
In my database, I am trying to count records from a field that meet the criteria of "SELECT DISTINCTROW WARes016.Stratum, Count((.)) AS CountOfDays
FROM WARes016 INNER JOIN ON WARes016.LRS = .LRS...
|
by: coool |
last post by:
I have this:
SELECT $items FROM $table WHERE $conditions
in my form I'm asking the user to select items from a list - multible selections
and I'm asking the user to select from which table...
|
by: denny1824 |
last post by:
I have a crystal report that someone else wrote and already has a Formula Field
if Criteria then (Count ({FirstField},{SecondField}) / Count ({FirstField}))
When the Criteria is true, this...
|
by: Coll |
last post by:
I have a form and a query. I would like to have some control on the
form (check box probably), that when selected will limit the criteria
for a field in a query. Here are the details....
When...
|
by: cmdolcet69 |
last post by:
I have the below if statement, that should catch if any of the
conditions are met.....however for some reasons if my
boolDSIFlushGapReading = true and MuxClass.DSIValues.count =1 and my...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |