Conditional counting

mikemathis

Board Regular
Joined
Jul 9, 2003
Messages
124
Office Version
  1. 2019
Platform
  1. Windows
I have a column of invoices which start with the year:

200602040005

where -
2006 is year
02 is month
05 is day
0005 is a sequential number

What I want to do is to take this list, which contains multiple years and count the number of instances where the year is 2006

I've tried to do it with CountIF and LEFT, but that's not working for me.

Any help out there?
many thanks.
Mike
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,210
=SUMPRODUCT((LEFT(A2:A400,4)+0=C2)+0)

where A2:A400 houses invoices and C2 the criterion year of 2006.
 

mikemathis

Board Regular
Joined
Jul 9, 2003
Messages
124
Office Version
  1. 2019
Platform
  1. Windows
Aladin,

Thanks for the formula... It's not working for me though. I checked and my invoices are formatted as numbers as is the cell for 2006.

I get a #VALUE! response.
 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,210
Aladin,

Thanks for the formula... It's not working for me though. I checked and my invoices are formatted as numbers as is the cell for 2006.

I get a #VALUE! response.

=IF(N(C2),SUMPRODUCT((LEFT(A2:A400&"0000",4)+0=C2)+0),"")
 

Brian from Maui

MrExcel MVP
Joined
Feb 16, 2002
Messages
8,459
I think the #VALUE error comes from empty cells in the range. You could simply use,

=SUMPRODUCT(--(LEFT(A1:A400,4)="2006"))
 

mikemathis

Board Regular
Joined
Jul 9, 2003
Messages
124
Office Version
  1. 2019
Platform
  1. Windows
I saw my problem... the range was bigger than the data - so I had some null values.

Is there a way to account for nulls? I'd like to pick a 'huge' range and then 'grow' into the formula.

Many thanks for walking me through this. I'm going to have to look up this SUMPRODUCT formula.
 

mikemathis

Board Regular
Joined
Jul 9, 2003
Messages
124
Office Version
  1. 2019
Platform
  1. Windows
Brian,

That works for an embedded year value (thanks!) but when I reference a cell with the year in it, it doesn't.
 

mikemathis

Board Regular
Joined
Jul 9, 2003
Messages
124
Office Version
  1. 2019
Platform
  1. Windows
Aladin,

Thanks. That does it exactly. I'm definitely going to have to get on top of SUMPRODUCT. Works for referenced cell! woohoo!
 

Brian from Maui

MrExcel MVP
Joined
Feb 16, 2002
Messages
8,459
Brian,

That works for an embedded year value (thanks!) but when I reference a cell with the year in it, it doesn't.

Because the LEFT function returns text, entering 2006 in a cell and using that cell as a reference will not work, as the cell that houses 2006 is numeric.
 

Forum statistics

Threads
1,176,530
Messages
5,903,602
Members
435,037
Latest member
AnneDerks

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
Top