Formula Help: Count Values within Unique Date Ranges

kkarp

New Member
Joined
Mar 25, 2016
Messages
8
Hello! Long time listener, first time caller.

I have a spreadsheet with rows for observations of individuals. The columns B-D indicate the dates of interest. Each date range in these columns is unique to the individual. The columns (G-L) are calendar dates -- there is a column for every day for 6 months. Individuals have an observation some days, and blanks other days. I want to know the number of observations in a given, unique date range for each individual.


I need help creating a formula that counts the number of values from Date X to Date Y (column E) and from Date X to Date Z (column F). Each row has a unique date range. I have filled out the an approximation of the sheet with what those formulas should return:


ABCDEFGHIJKL
1IDDate XDate YDate Z# Values from Date X to Date Y# Values from Date X to Date Z3/13/23/33/43/53/6
2993/13/33/524lmrslmrqrs
3773/23/43/634rslmrqlm

<tbody>
</tbody>










This spreadsheet will have several thousand individuals, each with unique date ranges, so ideally this formula would be drag-down-able. The codes (lm, rs, etc.) just need to be counted as one instance each.


I have attempted several COUNTIF functions, and have found difficulty since this formula must look at the date columns in the row of the individual as well as the date columns in the column headers. How can I do a matrixed count (if that that even a thing)? I'd be willing to try a VBA solution if folks think that is the best route.


Note:

I realize this might be more easily accomplished if I created a formula to pull down the date from the column header into the rows with the observation codes, like so:


ABCDEFGHIJKL
1IDDate XDate YDate Z# Values from Date X to Date Y# Values from Date X to Date Z3/13/23/33/43/53/6
2993/13/33/5243/13/33/43/53/6
3773/23/43/6343/23/33/43/6

<tbody>
</tbody>









For my column E formula, I could do: (COUNTIF(G3:L3,">="&B3)-COUNTIF(G3:L3,">="&C3))

The issue with this solution is that I really want to avoid adding formulas to practically every cell since this spreadsheet has over 100 columns and several thousand rows.


Cheers, and thanks for any thoughts you might have!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the MrExcel board!

Oh no, my formatting is horrific! Sorry, everyone!
See the link in my signature block below for help with that.


Try this formula in E2. Copy across to F2 and down the rows.

Excel Workbook
ABCDEFGHIJKL
1IDDate XDate YDate Z3-Jan3-Feb3-Mar3-Apr3-May3-Jun
2993-Jan3-Mar3-May24lmrslmrqrs
3773-Feb3-Apr3-Jun34rslmrqlm
Count
 
Last edited:
Upvote 0
Count

ABCDEFGHIJKL
1IDDate XDate YDate Z 3-Jan3-Feb3-Mar3-Apr3-May3-Jun
2993-Jan3-Mar3-May24lm rslmrqrs
3773-Feb3-Apr3-Jun34 rslmrq lm

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:33px;"><col style="width:57px;"><col style="width:59px;"><col style="width:57px;"><col style="width:26px;"><col style="width:26px;"><col style="width:50px;"><col style="width:52px;"><col style="width:52px;"><col style="width:50px;"><col style="width:55px;"><col style="width:50px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
E2=COUNTIFS($G$1:$L$1,">="&$B2,$G$1:$L$1,"<="&C2,$G2:$L2,"<>")

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4


Thank you so much, Peter. This very nearly works -- the only issue is that this formula returns a '0' value, but if I change any of the alphabetic codes into a number, then the formula returns a number. Do you have any hints on how to get the formula to recognize the alphabetic code as '1 instance'?
 
Upvote 0
the only issue is that this formula returns a '0' value, but if I change any of the alphabetic codes into a number, then the formula returns a number.
You can see that the formula is not returning 0 for me, with letters in G2:L3.

What exactly do you have in that range? If a formula, please give the formula for G2.

Can you post a small realistic sample using an HTML maker per the link I suggested? That way we can copy/paste and use the same data that you are having the problem with.

Edit: Also best not to post whole quotes. Just quote any small part(s) that you may be specifically referring to. It keeps the thread easier to read & navigate.
 
Last edited:
Upvote 0
Here is a small example of my dataset. In E2, I have the formula you developed. When I change the alphabetic codes to the number 1, the formula you've developed works correctly. G2 for the vast majority is blank, though in my earlier example that cell had a value -- I played around and added a value to G2 in my own sheet, and that had no effect on the formula. Thank you so much for your help -- I'm not sure why the formula is working for you and not for me.

Unique Count
Row\Col
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
AA
AB
AC
AD
AE
AF
AG
AH
AI
AJ
AK
AL
AM
AN
AO
AP
AQ
AR
AS
AT
AU
AV
AW
AX
AY
AZ
BA
BB
BC
BD
BE
BF
BG
BH
BI
BJ
BK
BL
BM
BN
BO
BP
BQ
BR
BS
BT
BU
BV
BW
BX
BY
BZ
CA
CB
CC
CD
CE
CF
CG
CH
CI
CJ
CK
CL
CM
CN
CO
CP
CQ
CR
CS
CT
CU
CV
CW
CX
CY
CZ
DA
DB
DC
DD
DE
DF
DG
DH
DI
DJ
DK
DL
DM
DN
DO
DP
DQ
DR
DS
DT
DU
DV
DW
DX
DY
DZ
EA
EB
EC
ED
EE
EF
EG
EH
EI
EJ
EK
EL
EM
EN
EO
EP
EQ
ER
ES
ET
EU
EV
EW
EX
EY
EZ
FA
FB
FC
FD
FE
FF
FG
FH
FI
FJ
FK
FL
FM
FN
FO
FP
FQ
FR
FS
FT
FU
FV
FW
FX
FY
FZ
GA
GB
GC
GD
GE
GF
GG
GH
GI
GJ
GK
GL
GM
GN
GO
GP
GQ
GR
GS
GT
GU
GV
GW
GX
GY
GZ
HA
HB
HC
HD
HE
HF
HG
HH
HI
HJ
HK
HL
HM
HN
HO
HP
HQ
HR
2
ID
Date X
Date Y
Date Z
# of Values between Date X and Date Y
# of Values between Date X and Date Z
8/17/15​
8/18/15​
8/19/15​
8/20/15​
8/21/15​
8/24/15​
8/25/15​
8/26/15​
8/27/15​
8/28/15​
8/31/15​
9/1/15​
9/2/15​
9/3/15​
9/4/15​
9/7/15​
9/8/15​
9/9/15​
9/10/15​
9/11/15​
9/14/15​
9/15/15​
9/16/15​
9/17/15​
9/18/15​
9/21/15​
9/22/15​
9/23/15​
9/24/15​
9/25/15​
9/28/15​
9/29/15​
9/30/15​
10/1/15​
10/2/15​
10/5/15​
10/6/15​
10/7/15​
10/8/15​
10/9/15​
10/12/15​
10/13/15​
10/14/15​
10/15/15​
10/16/15​
10/19/15​
10/20/15​
10/21/15​
10/22/15​
10/23/15​
10/26/15​
10/27/15​
10/28/15​
10/29/15​
10/30/15​
11/2/15​
11/3/15​
11/4/15​
11/5/15​
11/6/15​
11/9/15​
11/10/15​
11/11/15​
11/12/15​
11/13/15​
11/16/15​
11/17/15​
11/18/15​
11/19/15​
11/20/15​
11/23/15​
11/24/15​
11/25/15​
11/26/15​
11/27/15​
11/30/15​
12/1/15​
12/2/15​
12/3/15​
12/4/15​
12/7/15​
12/8/15​
12/9/15​
12/10/15​
12/11/15​
12/14/15​
12/15/15​
12/16/15​
12/17/15​
12/18/15​
12/21/15​
12/22/15​
12/23/15​
12/24/15​
12/25/15​
12/28/15​
12/29/15​
12/30/15​
12/31/15​
1/1/16​
1/4/16​
1/5/16​
1/6/16​
1/7/16​
1/8/16​
1/11/16​
1/12/16​
1/13/16​
1/14/16​
1/15/16​
1/18/16​
1/19/16​
1/20/16​
1/21/16​
1/22/16​
1/25/16​
1/26/16​
1/27/16​
1/28/16​
1/29/16​
2/1/16​
2/2/16​
2/3/16​
2/4/16​
2/5/16​
2/8/16​
2/9/16​
2/10/16​
2/11/16​
2/12/16​
2/15/16​
2/16/16​
2/17/16​
2/18/16​
2/19/16​
2/22/16​
2/23/16​
2/24/16​
2/25/16​
2/26/16​
2/29/16​
3/1/16​
3/2/16​
3/3/16​
3/4/16​
3/7/16​
3/8/16​
3/9/16​
3/10/16​
3/11/16​
3/14/16​
3/15/16​
3/16/16​
3/17/16​
3/18/16​
3/21/16​
3/22/16​
3/23/16​
3/24/16​
3/25/16​
3/28/16​
3/29/16​
3/30/16​
3/31/16​
4/1/16​
4/4/16​
4/5/16​
4/6/16​
4/7/16​
4/8/16​
4/11/16​
4/12/16​
4/13/16​
4/14/16​
4/15/16​
4/18/16​
4/19/16​
4/20/16​
4/21/16​
4/22/16​
4/25/16​
4/26/16​
4/27/16​
4/28/16​
4/29/16​
5/2/16​
5/3/16​
5/4/16​
5/5/16​
5/6/16​
5/9/16​
5/10/16​
5/11/16​
5/12/16​
5/13/16​
5/16/16​
5/17/16​
5/18/16​
5/19/16​
5/20/16​
5/23/16​
5/24/16​
5/25/16​
5/26/16​
5/27/16​
5/30/16​
5/31/16​
6/1/16​
6/2/16​
6/3/16​
6/6/16​
6/7/16​
6/8/16​
6/9/16​
6/10/16​
6/13/16​
6/14/16​
6/15/16​
6/16/16​
6/17/16​
3
723456​
9/17/15​
12/14/15​
3/17/15​
0​
Count of values in Row 3 within the range of Row 3's Date X and Date Z, highlighted in yellow and purpleRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTRTNTNTRTRTRTRTMSTSTRTMRTRTRTRTRTRTRTRTNTNTRT

<tbody>
</tbody>
 
Upvote 0
=COUNTIFS($G$2:$HR$2,">="&$B3,$G$3:$HR$3,"<="&C3,$G3:$HR3,"<>")

I did edit the formula to account for the true column headers -- have I made an error in modifying the formula?
 
Upvote 0
I think you meant to put this:

Code:
[COLOR=#333333]=COUNTIFS($G$2:$HR$2,">="&$B3,$G$2:$HR$2,"<="&C3,$G3:$HR3,"<>")[/COLOR]
 
Upvote 0
@kkarp
cb366374 has corrected the error that you made with the formula for E3.

There will still be an issue with the F3 formula. It would be normal expectation that when you talk about counting values between Date X and Date Z (which you did in post #1) that those two dates would be in that chronological order. That is, date X would come before Date Z, and that was true for the samples you gave in post #1.

However, that does not appear to be the case with your sample in post #6.
17 September 2015 (Date X) is after 17 March 2015 (Date Z).

So the first thing to determine: Is your sample in post #7 an error, or are your samples in post #1 errors, or can the dates be in any order?
 
Upvote 0
@kkarp
cb366374 has corrected the error that you made with the formula for E3.

So the first thing to determine: Is your sample in post #7 an error, or are your samples in post #1 errors, or can the dates be in any order?

Thanks Peter and cb366374! You two are fantastic.

To answer your question, yes, the date there was one I had randomly generated and I did not check to see that the dates were in chronological order -- the sample post in #7 did contain an error.

I've used the formula that cb366374 corrected and it seems to be working perfectly in column E and I dragged it over to column F and that appears to be working correctly as well.

Thank you again so much! This was a challenging one for me -- resounding high-fives to the both of you!
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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