countifs function; need to set a date range as a criteria

marco10

New Member
Joined
Mar 8, 2020
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,
so i am trying to use countifs function to count number of people with multiple criteria.

this is the formula i am using:
=-COUNTIFS($C:$C,"Controller",$D:$D,"temp",$E:$E,"may-20",$F:$F, "may-20")
[(=-countifs(criteria range1,"criteria1",criteria range2,"criteria2",criteria range3,"criteria3",criteria range4,"criteria4")]
this uses the person's job role, contract type, start date and end date as criteria.

  • Previously, it looked like it was working well but then i realized if the start date is anything different than 01-may-20 similarly the end date if anything different from 01-may-20, it won't count as the criteria is not fulfilled and i get 0 as answer.
  • I tried using ">=may-20" and also changed to "=>may-20" but it would only consider the sign exactly infront of may; sign at the very beginning of the criteria is ignored.
  • "=01-may-20:31-may-20" tried this but was unsuccessful to achieve the desired result.
  • in the above experiment date was set to 15-may-20.
  • When ">" sign is used all the date of the month are counted but 01-may-20 will be ignored.
  • I need a way to put the date range as a criteria so that any date of that particular month is okay to fulfill the criteria.
will be grateful for the help, thank you.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
It sounds like you want to count all of the rows where the start date __and__ end date are in the same month, namly May 2020. Right?

And it sounds like the data in columns E and F are bona fide dates, not text. Right?

Confirm that ISNUMBER(E1) and ISNUMBER(F1) both return TRUE in all of the rows to be counted.

To that end, the following should work:

=-COUNTIFS(C1:C1000, "Controller", D1:D1000, "temp", E1:E1000, ">=" & DATE(2020,5,1), F1:F1000, "<" & DATE(2020,6,1))

In other words, count each row where column C is "controller", __and__ column D is "temp", __and__ the value in column E is >= DATE(2020,5,1), __and__ the value in column F is < DATE(2020,6,1).

Please note some subtle unrelated changes:

1. I use a limited range like C1:C1000 instead of a whole-column reference like C:C. Change C1 and C1000 to first and last cells where you might reasonably expect relevant data now and in the future. With a whole-column range like C:C, Excel might check as many as 1+ million rows. I presume that it is not "reasonable" to expect that many rows of data.

2. All conditions are AND'ed. If, instead, you want to count were a May date is in either column E __or__ column F, not necessarily both, you need very different formulas. LMK.

3. I use DATE(2020,5,1) instead of May-20, for example, to unambiguously refer to the dates that you want.

If that does not work for you, provide example data and corresponding COUNTIFS formula. As far as I'm concerned, you can upload an example file that demonstrates the problem to a file-sharing website, and post the download URL. Others active contributes might prefer that you use XL2BB feature in this forum.
 
Upvote 0
Personally I would break it up into modular steps to separate the date filtering step from the countif function. For example, set a flag to 1 if within a date range then use countif to process a much simpler query (countif x = 1).

This will need more code but it will take less time to prepare and it will also allow for easy modification of the code in the future. Less code is not always the best but good luck with whichever path you take.
 
Upvote 0
=-COUNTIFS(C1:C1000, "Controller", D1:D1000, "temp", E1:E1000, ">=" & DATE(2020,5,1), F1:F1000, "<" & DATE(2020,6,1))

The above works for May 2020. Equivalently, we could write "<=" & DATE(2020,5,31) for the last condition.

And the following demonstrates how to do it if we want the date to be a cell reference.

Book1
ABCDEF
1-1May-2020controller03-Jan-202028-May-2020
2-1Jun-2020controller09-Feb-202027-Aug-2020
3controller29-May-202031-May-2020
4controller25-May-202001-Dec-2020
5temp24-Jan-202021-Sep-2020
6temp22-Jan-202012-May-2020
7temp18-May-202023-May-2020
8temp20-May-202006-Dec-2020
9controllertemp20-Apr-202027-Jul-2020
10controllertemp03-Jun-202023-Jun-2020
11controllertemp01-May-202029-Sep-2020
12controllertemp26-May-202030-May-2020
Sheet1

Rich (BB code):
Formulas:
A1: =-COUNTIFS($C$1:$C$1000, "Controller", $D$1:$D$1000, "temp", $E$1:$E$1000, ">=" & B1, $F$1:$F$1000, "<=" & EOMONTH(B1,0))
A2: =-COUNTIFS($C$1:$C$1000, "Controller", $D$1:$D$1000, "temp", $E$1:$E$1000, ">=" & B2, $F$1:$F$1000, "<=" & EOMONTH(B2,0))

B1 and B2 are first-of-the-month dates, to wit: 1-May-2020 and 1-June-2020. They are formatted to display May-2020 and Jun-2020.
 
Upvote 0
first of all, Apologies for my delayed response had to make some repairs.
thank you so much for helping me through you busy schedule.
to make it easier to discuss i have downloaded xl2bb to post the range rather than the photo.
so its like a data entry sheet and then an output sheet as follows.

updated crossed out base of Pre-contract staff database 06-04-20.xlsx
BCDEF
1last nameRoletemp/permstart date end date
100XEstimating Manager-CivilTempJan-20Jun-20
101Y Estimating Manager-CivilTempMar-20Jun-20
102ZEstimating Manager-CivilTempJan-20May-20
Staff Entry (civil & rail only)


and now the output sheet:

updated crossed out base of Pre-contract staff database 06-04-20.xlsx
CDEFGHIJKLMNO
1Temp staff
2Job rolesJan-20Feb-20Mar-20Apr-20May-20Jun-20Jul-20Aug-20Sep-20Oct-20Nov-20Dec-20
3Estimating Manager-Civil-2-2-3-3-3-2000000
Sheet1
Cell Formulas
RangeFormula
D3D3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"jan-20",'Staff Entry (civil & rail only)'!$F:$F, ">jan-20")
E3E3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=feb-20",'Staff Entry (civil & rail only)'!$F:$F, ">feb-20")
F3F3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=Mar-20",'Staff Entry (civil & rail only)'!$F:$F, ">mar-20")
G3G3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=apr-20",'Staff Entry (civil & rail only)'!$F:$F, ">apr-20")
H3H3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=may-20",'Staff Entry (civil & rail only)'!$F:$F, ">may-20")
I3I3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=jun-20",'Staff Entry (civil & rail only)'!$F:$F, ">jun-20")
J3J3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=jul-20",'Staff Entry (civil & rail only)'!$F:$F, ">jul-20")
K3K3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=aug-20",'Staff Entry (civil & rail only)'!$F:$F, ">aug-20")
L3L3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=sep-20",'Staff Entry (civil & rail only)'!$F:$F, ">sep-20")
M3M3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=oct-20",'Staff Entry (civil & rail only)'!$F:$F, ">oct-20")
N3N3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=nov-20",'Staff Entry (civil & rail only)'!$F:$F, ">nov-20")
O3O3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=dec-20",'Staff Entry (civil & rail only)'!$F:$F, ">=dec-20")


this is the very initial work done by me which was fine until i realized that it won't work if the date in cell E of data entry is more than 01-Jan-2020 and also if the date in cell F is exactly 01-any month-2020; i:e has to be after 1st of any month for the formula to work.
 
Upvote 0
now without changing anything in the data entry sheet i made a change in the output sheet; i:e for the month of jan however, the result is 0.
the result:

updated crossed out base of Pre-contract staff database 06-04-20.xlsx
CDEFGHIJKLMNO
1Temp staff
2Job rolesJan-20Feb-20Mar-20Apr-20May-20Jun-20Jul-20Aug-20Sep-20Oct-20Nov-20Dec-20
3Estimating Manager-Civil0-2-3-3-3-2000000
Sheet1
Cell Formulas
RangeFormula
D3D3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,">=" & DATE(2020,1,1),'Staff Entry (civil & rail only)'!$F:$F, "<" & DATE(2020,2,1))
E3E3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=feb-20",'Staff Entry (civil & rail only)'!$F:$F, ">feb-20")
F3F3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=Mar-20",'Staff Entry (civil & rail only)'!$F:$F, ">mar-20")
G3G3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=apr-20",'Staff Entry (civil & rail only)'!$F:$F, ">apr-20")
H3H3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=may-20",'Staff Entry (civil & rail only)'!$F:$F, ">may-20")
I3I3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=jun-20",'Staff Entry (civil & rail only)'!$F:$F, ">jun-20")
J3J3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=jul-20",'Staff Entry (civil & rail only)'!$F:$F, ">jul-20")
K3K3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=aug-20",'Staff Entry (civil & rail only)'!$F:$F, ">aug-20")
L3L3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=sep-20",'Staff Entry (civil & rail only)'!$F:$F, ">sep-20")
M3M3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=oct-20",'Staff Entry (civil & rail only)'!$F:$F, ">oct-20")
N3N3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=nov-20",'Staff Entry (civil & rail only)'!$F:$F, ">nov-20")
O3O3=-COUNTIFS('Staff Entry (civil & rail only)'!$C:$C,"Estimating Manager-Civil",'Staff Entry (civil & rail only)'!$D:$D,"temp",'Staff Entry (civil & rail only)'!$E:$E,"<=dec-20",'Staff Entry (civil & rail only)'!$F:$F, ">=dec-20")


once again thank you @joeu2004 and @Jerrod Conomy, i really appreciate your help and am grateful for the continuous support that you experts provide to this community.
have a good day, stay safe. : )
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top