Help with VBA code to replace COUNTIFS formula that is crashing Excel

Joyner

Well-known Member
Joined
Nov 15, 2005
Messages
1,202
Hello everyone and thank you for the help.

I am trying to copy this formula down a column to count certain occurrences and I need to copy it down for up to 400k rows:

=COUNTIFS($O$4:O4,O4,$W$4:W4,W4,$AB$4:AB4,AB4)

Note the ranges are expanding as the formula is copied down.

For whatever reason this particular formula crashes Excel after giving me the "Out of Memory" error - I assuming Excel's resources don't appreciate 400K countifs formulas.

I am just a beginner at VBA, can anyone point me in the right direction about how I would write code to count the occurrences I am trying to do with the COUNTIFS formula and put the number in a column, if it matters, I would like the numbers to go in column AC, starting in row 4.

Thank you
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try the pivot table instead. Powerpivot is even better for this.

Thanks, but I don't think a pivot table will give me the type of count I am looking for with the expanding ranges and multiple criteria I need and I need to use the count for other things also.
 
Upvote 0
It can handle expanding ranges/multi criteria, post a smaller sample and I'll show you. Otherwise powerpivot, power query, or SQL will definitely work.
 
Upvote 0
Thanks, I would like to see how that is done. I am pretty good with Pivot Tables but I can't envision how a Pivot Table could do what I need. Power Pivot and Power Query I am just now learning and could see maybe inside the data model it is possible but again I would have no idea how to do that.

For posting the sample, I'll see if I can - maybe there are options for me to get a sample posted, but my work has our computers locked down tight and we can't add any programs.

Thanks again for the help.
 
Upvote 0
[TABLE="class: grid"]
<tbody>[TR]
[TD="class: xl65"]Id
[/TD]
[TD="class: xl65"]Name
[/TD]
[TD="class: xl65"]Date
[/TD]
[TD="class: xl65"]Amount
[/TD]
[TD="class: xl65, width: 96, bgcolor: #5B9BD5"]Occurrence
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00001
[/TD]
[TD="class: xl67"]Smith, John
[/TD]
[TD="class: xl68, align: right"]10/31/2014
[/TD]
[TD="class: xl67, align: right"]-1286.4
[/TD]
[TD="class: xl67, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00002
[/TD]
[TD="class: xl70, bgcolor: transparent"]Doe, Jane
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]9/5/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]-129.11
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00001
[/TD]
[TD="class: xl67"]Smith, John
[/TD]
[TD="class: xl68, align: right"]10/31/2014
[/TD]
[TD="class: xl67, align: right"]1286.4
[/TD]
[TD="class: xl67, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00002
[/TD]
[TD="class: xl70, bgcolor: transparent"]Doe, Jane
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]9/5/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]129.11
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00001
[/TD]
[TD="class: xl67"]Smith, John
[/TD]
[TD="class: xl68, align: right"]10/31/2014
[/TD]
[TD="class: xl67, align: right"]-1286.4
[/TD]
[TD="class: xl67, align: right"]2
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00002
[/TD]
[TD="class: xl70, bgcolor: transparent"]Doe, Jane
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]9/5/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]-129.11
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]2
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00002
[/TD]
[TD="class: xl67"]Doe, Jane
[/TD]
[TD="class: xl68, align: right"]9/5/2014
[/TD]
[TD="class: xl67, align: right"]129.11
[/TD]
[TD="class: xl67, align: right"]2
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00001
[/TD]
[TD="class: xl70, bgcolor: transparent"]Smith, John
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]10/31/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]1286.4
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]2
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00003
[/TD]
[TD="class: xl67"]Deal, Big
[/TD]
[TD="class: xl68, align: right"]12/5/2014
[/TD]
[TD="class: xl67, align: right"]-5961.54
[/TD]
[TD="class: xl67, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00002
[/TD]
[TD="class: xl70, bgcolor: transparent"]Doe, Jane
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]9/5/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]-129.11
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]3
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00001
[/TD]
[TD="class: xl67"]Smith, John
[/TD]
[TD="class: xl68, align: right"]10/31/2014
[/TD]
[TD="class: xl67, align: right"]-1286.4
[/TD]
[TD="class: xl67, align: right"]3
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00004
[/TD]
[TD="class: xl70, bgcolor: transparent"]Too, Me
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]10/10/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]-170.79
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00001
[/TD]
[TD="class: xl67"]Smith, John
[/TD]
[TD="class: xl68, align: right"]10/31/2014
[/TD]
[TD="class: xl67, align: right"]-1286.4
[/TD]
[TD="class: xl67, align: right"]4
[/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: transparent"]00001
[/TD]
[TD="class: xl70, bgcolor: transparent"]Smith, John
[/TD]
[TD="class: xl71, bgcolor: transparent, align: right"]12/5/2014
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]-121
[/TD]
[TD="class: xl70, bgcolor: transparent, align: right"]1
[/TD]
[/TR]
[TR]
[TD="class: xl66"]00001
[/TD]
[TD="class: xl67"]Smith, John
[/TD]
[TD="class: xl68, align: right"]10/24/2014
[/TD]
[TD="class: xl67, align: right"]-490.06
[/TD]
[TD="class: xl67, align: right"]1
[/TD]
[/TR]
</tbody>[/TABLE]


Wow, look at me, I did it!

So Sheetspread, hopefully you have enough here to see what I need. I need the occurrence column to count the occurrence of each positive and each negative, I am matching offsets. Let me know if you need anything else.
 
Last edited:
Upvote 0
You'll just have to add an order field to the source data, then insert a PT, drag all the fields in row labels, and also amount into values, change the value setting to count, and running total off order:


Excel 2010
ABCDEFGHIJKLM
1OrderIdNameDateAmountOccurrence
211Smith, John10/31/2014-1286.41OrderIdNameDateAmountCount of Amount
322Doe, Jane9/5/2014-129.11111Smith, John10/31/2014-1286.41
431Smith, John10/31/20141286.4122Doe, Jane9/5/2014-129.111
542Doe, Jane9/5/2014129.11131Smith, John10/31/20141286.41
651Smith, John10/31/2014-1286.4242Doe, Jane9/5/2014129.111
762Doe, Jane9/5/2014-129.11251Smith, John10/31/2014-1286.42
872Doe, Jane9/5/2014129.11262Doe, Jane9/5/2014-129.112
981Smith, John10/31/20141286.4272Doe, Jane9/5/2014129.112
1093Deal, Big12/5/2014-5961.5181Smith, John10/31/20141286.42
11102Doe, Jane9/5/2014-129.11393Deal, Big12/5/2014-5961.541
12111Smith, John10/31/2014-1286.43102Doe, Jane9/5/2014-129.113
13124Too, Me10/10/2014-170.791111Smith, John10/31/2014-1286.43
14131Smith, John10/31/2014-1286.44124Too, Me10/10/2014-170.791
15141Smith, John12/5/2014-1211131Smith, John10/31/2014-1286.44
16151Smith, John10/24/2014-490.061141Smith, John12/5/2014-1211
17151Smith, John10/24/2014-490.061
Sheet6
 
Upvote 0
Wow, brilliant sheetspread, works perfect - I would have never thought about a running total of a sequence number (order field) and think I am still trying to wrap my head around the logic...

Thanks again, I learned something valuable today...
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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