Unable to use YEAR() and MONTH() inside COUNTIF()

%3CLINGO-SUB%20id%3D%22lingo-sub-113555%22%20slang%3D%22en-US%22%3EUnable%20to%20use%20YEAR()%20and%20MONTH()%20inside%20COUNTIF()%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-113555%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20count%20and%20sum%20a%20column%20of%20numbers%20based%20on%20month%20and%20year.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20most%20simplest%20example%2C%20I%20made%20a%20blank%20workbook%20with%20a%20list%20of%20dates%20and%20then%20tried%20to%20count%20them%20based%20on%20month%20and%20it%20just%20returns%200.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EColumn%20A%3A%3C%2FP%3E%3CP%3E%26nbsp%3B10%2F1%2F2017%3CBR%20%2F%3E10%2F1%2F2017%3CBR%20%2F%3E10%2F2%2F2017%3CBR%20%2F%3E10%2F3%2F2016%3CBR%20%2F%3E10%2F1%2F2016%3CBR%20%2F%3E9%2F1%2F2017%3CBR%20%2F%3E9%2F1%2F2016%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3DCOUNTIF(A%3AA%2C%20%22MONTH(A%3AA)%3D10%22)%3C%2FP%3E%3CP%3E...is%20returning%200.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-113555%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20%26amp%3B%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENeed%20Help%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-113731%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20use%20YEAR()%20and%20MONTH()%20inside%20COUNTIF()%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-113731%22%20slang%3D%22en-US%22%3E%3CP%3ETim%2C%20bit%20more%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20the%26nbsp%3BCOUNTIF%20do%20is%20compare%20one%20by%20one%20A1%2C%20A2%2C%20etc%20with%20criteria%20you%20set%20by%20second%20parameter.%20In%20your%20case%20that's%20some%20string%2C%20no%20one%20day%20(which%20is%20actually%20the%20integer%20number)%20is%20equal%20to%20any%20string.%20Thus%20result%20is%20zero.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20you%20can't%20apply%20any%20formula%20to%20first%20parameter%2C%20something%20like%20%3DCOUNTIF(MONTH(A%3AA)%2C10)%20returns%20an%20error.%20First%20parameter%20shall%20be%20a%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20calculate%20months%20for%20any%20year%20usual%20alternative%2C%20as%20Bryant%20suggested%2C%20is%20using%20of%20SUMPRODUCT.%20Here%20you%20may%20use%20calculation%20on%20range%3C%2FP%3E%3CPRE%3E0%20%3DCOUNTIF(A%3AA%2C%22MONTH(A%3AA)%3D10%22)%0A5%20%3DSUMPRODUCT((MONTH(A%3AA)%3D10)*1)%0A5%20%7B%3DSUM(IF(MONTH(A%3AA)%3D10%2C1))%7D%0A5%20%7B%3DSUM(--(MONTH(A%3AA)%3D10))%7D%0A3%20%3DCOUNTIFS(A%3AA%2C%22%26lt%3B%3D%22%26amp%3BEOMONTH(DATE(2017%2C10%2C1)%2C0)%2CA%3AA%2C%22%26gt%3B%22%26amp%3BEOMONTH(DATE(2017%2C10%2C1)%2C-1))%0A3%20%3DSUMPRODUCT((MONTH(A%3AA)%3D10)*(YEAR(A%3AA)%3D2017))%3C%2FPRE%3E%3CP%3EPractically%26nbsp%3Bthat's%20the%20equivalent%20of%20third%20formula%2C%20or%20in%20more%20compact%20form%20forth%20one%2C%20but%20they%20are%20to%20be%20used%20as%20array%20formulas.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20if%20your%20criteria%26nbsp%3Bis%20the%20month%20for%20the%20concrete%20year%20(other%20words%20combination%20of%20the%20year%20and%20month)%20you%20may%20use%20COUNTIFS%20as%20above%20(or%20latest%20SUMPRODUCT%20which%20returns%20the%20same)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-113572%22%20slang%3D%22en-US%22%3ERe%3A%20Unable%20to%20use%20YEAR()%20and%20MONTH()%20inside%20COUNTIF()%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-113572%22%20slang%3D%22en-US%22%3E%3CP%3EHere's%20an%20option%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fanswers.microsoft.com%2Fen-us%2Foffice%2Fforum%2Foffice_2003-excel%2Fcount-if-date-is-in-month%2F8d0367be-c619-4ad3-b350-2a1f86a3bbaf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fanswers.microsoft.com%2Fen-us%2Foffice%2Fforum%2Foffice_2003-excel%2Fcount-if-date-is-in-month%2F8d0367be-c619-4ad3-b350-2a1f86a3bbaf%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Senior Member

I am trying to count and sum a column of numbers based on month and year.

 

In the most simplest example, I made a blank workbook with a list of dates and then tried to count them based on month and it just returns 0.

 

Column A:

 10/1/2017
10/1/2017
10/2/2017
10/3/2016
10/1/2016
9/1/2017
9/1/2016

 

=COUNTIF(A:A, "MONTH(A:A)=10")

...is returning 0.

 

 

4 Replies

Tim, bit more

 

What the COUNTIF do is compare one by one A1, A2, etc with criteria you set by second parameter. In your case that's some string, no one day (which is actually the integer number) is equal to any string. Thus result is zero.

 

And you can't apply any formula to first parameter, something like =COUNTIF(MONTH(A:A),10) returns an error. First parameter shall be a range.

 

If calculate months for any year usual alternative, as Bryant suggested, is using of SUMPRODUCT. Here you may use calculation on range

0	=COUNTIF(A:A,"MONTH(A:A)=10")
5	=SUMPRODUCT((MONTH(A:A)=10)*1)
5	{=SUM(IF(MONTH(A:A)=10,1))}
5	{=SUM(--(MONTH(A:A)=10))}
3	=COUNTIFS(A:A,"<="&EOMONTH(DATE(2017,10,1),0),A:A,">"&EOMONTH(DATE(2017,10,1),-1))
3	=SUMPRODUCT((MONTH(A:A)=10)*(YEAR(A:A)=2017))

Practically that's the equivalent of third formula, or in more compact form forth one, but they are to be used as array formulas.

 

However, if your criteria is the month for the concrete year (other words combination of the year and month) you may use COUNTIFS as above (or latest SUMPRODUCT which returns the same)

 

@Sergei Baklan 

 

I am trying to sum colum AX if column AN is equal to Month (Jan) and Year (2020) and column AL equal to certain installer name. I have tried =SUMIFS(AX7:AX, AL7:AL,B65,AN7:AN, ">="&DATE(YEAR(D61),MONTH(G613),1)) B65 is variable for installer name, D61 is for year 2020 and G613 is for intended month january but it's not working all throughout the table.

 

_Eve26_0-1637573144875.png

 

How can I fix this. Thanks.

 

@_Eve26 

Did you try more exact ranges, like AX7:AX10000, etc.?