473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

COUNT.IF conditions/CRITERIA

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.
May 31 '11 #1
6 1956
Guido Geurs
767 Expert 512MB
This macro will do it.

Expand|Select|Wrap|Line Numbers
  1. Sub Macro1()
  2. Dim COUNTER As Integer
  3.     Range("A1").Activate
  4.     Do While ActiveCell.Value <> ""
  5.         With ActiveCell
  6.             If .Value = 11 Or _
  7.                 (.Value >= 55 And .Value <= 71) Or _
  8.                 .Value >= 83 Then _
  9.                     COUNTER = COUNTER + 1
  10.             .Offset(0, 1).Activate
  11.         End With
  12.     Loop
  13.     Range("A2").Value = COUNTER
  14. End Sub
Jun 2 '11 #2
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
Jun 2 '11 #3
MikeTheBike
639 Expert 512MB
Hi

You can create your own user define function, Say ValuesCount()

Which could look something like this
Expand|Select|Wrap|Line Numbers
  1. Function ValuesCount(ByRef ThisRange As Range) As Integer
  2.     Dim cel As Range
  3.  
  4.     For Each cel In ThisRange
  5.         If IsNumeric(cel) Then
  6.             Select Case True
  7.                 Case cel.Value = 11
  8.                     ValuesCount = ValuesCount + 1
  9.                 Case cel.Value >= 83
  10.                     ValuesCount = ValuesCount + 1
  11.                 Case (cel.Value >= 55 And cel.Value <= 75)
  12.                     ValuesCount = ValuesCount + 1
  13.             End Select
  14.         End If
  15.     Next cel
  16. 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
Jun 3 '11 #4
Guido Geurs
767 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. Sub COUNT()
  2. Dim ARRAYDATA As Variant '§ with range=>array must ALLWAYS be variant.
  3. Dim ARRAYCOUNTER() As Integer '§ count results for each line
  4. Dim ROW As Integer
  5. Dim COL As Integer
  6. Dim COUNTER As Integer
  7. Dim TOTAL As Integer
  8. '§ put sheet1 in array
  9.     Sheets(1).Select
  10. '§                                ( |------rows--------------|,|-----cols----------------------| )
  11.     ARRAYDATA = Range("A1").Resize(Range("A1").End(xlDown).ROW, Range("A1").End(xlToRight).Column)
  12. '§ count each row
  13.     ReDim ARRAYCOUNTER(1 To UBound(ARRAYDATA, 1), 1 To 1)
  14.     For ROW = 1 To UBound(ARRAYDATA, 1)
  15.         COUNTER = 0
  16.         For COL = 1 To UBound(ARRAYDATA, 2)
  17.             If ARRAYDATA(ROW, COL) = 11 Or _
  18.                 (ARRAYDATA(ROW, COL) >= 55 And ARRAYDATA(ROW, COL) <= 71) Or _
  19.                 ARRAYDATA(ROW, COL) >= 83 Then _
  20.                     COUNTER = COUNTER + 1
  21.         Next
  22.         ARRAYCOUNTER(ROW, 1) = COUNTER
  23.     Next
  24. '§ copy ARRAYCOUNTER to sheet
  25.     Range("L1").Resize(UBound(ARRAYCOUNTER), 1) = ARRAYCOUNTER
  26. '§ calculate TOTAL
  27.     TOTAL = 0
  28.     For ROW = 1 To UBound(ARRAYCOUNTER, 1)
  29.         TOTAL = TOTAL + ARRAYCOUNTER(ROW, 1)
  30.     Next
  31. '§ put TOTAL in sheet
  32.     Range("L" & CStr((UBound(ARRAYCOUNTER, 1) + 2))).Value = TOTAL
  33. End Sub
Attached Files
File Type: zip COUNT.IF conditions CRITERIA_v1.zip (9.6 KB, 58 views)
Jun 3 '11 #5
@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
Jun 3 '11 #6
Guido Geurs
767 Expert 512MB
this will add the explicit numbers to the sheet: see attachment
Attached Files
File Type: zip COUNT.IF conditions CRITERIA_v2.zip (13.8 KB, 53 views)
Jun 4 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
8
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. ...
6
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...
2
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...
3
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...
10
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...
18
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...
0
denny1824
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...
2
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...
16
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...
0
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$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.