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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
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
Id
Name
Date
Amount
Occurrence
00001
Smith, John
10/31/2014
-1286.4
1
00002
Doe, Jane
9/5/2014
-129.11
1
00001
Smith, John
10/31/2014
1286.4
1
00002
Doe, Jane
9/5/2014
129.11
1
00001
Smith, John
10/31/2014
-1286.4
2
00002
Doe, Jane
9/5/2014
-129.11
2
00002
Doe, Jane
9/5/2014
129.11
2
00001
Smith, John
10/31/2014
1286.4
2
00003
Deal, Big
12/5/2014
-5961.54
1
00002
Doe, Jane
9/5/2014
-129.11
3
00001
Smith, John
10/31/2014
-1286.4
3
00004
Too, Me
10/10/2014
-170.79
1
00001
Smith, John
10/31/2014
-1286.4
4
00001
Smith, John
12/5/2014
-121
1
00001
Smith, John
10/24/2014
-490.06
1

<tbody>
</tbody>


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,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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