Convert a Countifs with ?array? sum to VBA

yakfan

New Member
Joined
Mar 31, 2013
Messages
4
I have the following set of test data:

ABCDEFGHI
1IDsvc fromsvc tosvc 1 & 2count
IDsvc fromsvc tosvc code
211/1/20101/31/2010311/1/20101/31/20101
312/1/20102/28/2010311/1/20101/31/20103
422/1/20102/28/2010311/1/20101/31/20102
523/1/20103/31/2010211/1/20101/31/20101
624/1/20104/30/2010112/1/20102/28/20105
734/1/20104/30/2010012/1/20102/28/20101
835/1/20105/31/2010312/1/20102/28/20102
936/1/20106/30/2010112/1/20102/28/20101
1047/1/20107/31/2010112/1/20102/28/20104
1157/1/20107/31/2010222/1/20102/28/20101
1222/1/20102/28/20105
1322/1/20102/28/20101
1422/1/20102/28/20102
1522/1/20102/28/20104
1622/1/20102/28/20104
1723/1/20103/31/20102
1823/1/20103/31/20103
1923/1/20103/31/20101
2023/1/20103/31/20104
2124/1/20104/30/20102
2234/1/20104/30/20103
2334/1/20104/30/20104
2434/1/20104/30/20105
2535/1/20105/31/20102
2635/1/20105/31/20102
2735/1/20105/31/20102
2836/1/20106/30/20102
2936/1/20106/30/20104
3047/1/20107/31/20101
3147/1/20107/31/20105
3247/1/20107/31/20104
3357/1/20107/31/20102
3457/1/20107/31/20104
3557/1/20107/31/20104
3657/1/20107/31/20104
3757/1/20107/31/20101

<tbody>
</tbody>

Cell D2 =SUM(COUNTIFS($F$2:$F$37,A2,$G$2:$G$37,B2,$H$2:$H$37,C2,$I$2:$I$37,{1,2}))

Lets just assume that I have a formula =SUM(Countifs(I2:I37,{1,2})), how would I write that to VBA?
I can probably handle the rest. I just have no clue how to use arrays this way in VBA or how to begin looking to learn.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
in VBA if you want to write a formula to a cell, you use
Code:
cell(2,4).formula="Sum(blah,blah)"
for arrayformulas you use
Code:
cell(2,4).formulaArray="Sum(blah,blah)"
 
Upvote 0
If you need to fill out the whole column D with the formula it is quicker to use RC references, becaus you can then set the range to the formula in one sweep.
 
Upvote 0
Thanks. But I'm sorry, I forgot to mention that I do not want to input a formula into the cell, but be able to evaluate the expression and determine a value using VBA. This value/variable will then be evaluated to return a simple Yes or No.

Additional details you may not need to know:

My final product will be to put this within a set of nested if statements to evaluate different aspects of the data to return a Yes or No. (Would not want it to say "Yes", "Yes", "No", [long formula], "Yes"). Also, I want to be able to accommodate the user specifying what "svc codes" out of hundreds they want to count.

I'm not sure if I can actually use RC referencing. In the end, there are a bunch of dynamically changing factors I would be accommodating for. The "svc from" column may or may not be located in B. I'm using Match() to determine where each column is and will be referencing in that manner, so my column references would be "2" rather than "B". The final output would also be displayed on the "LastColumn +1". All this probably makes it significantly more challenging to shove in a formula into the cell and dealing with additional referencing factors.
 
Upvote 0
Ah, understand.

Well you can Dim a variant and then load the complete table into the variant. This will give you an array, two dimensional, one withthe rows, the other with the columns.

Now you can do your calculations on the elements in the array, which is a lot quicker than doing it on the sheet. You can even use Application.WorksheetFunction to access all functions and work them on the array.

Or you can loop through all the elements of a row or column in the array and test the values and sum them. Because it doesn't read off the sheet it is very fast.

There will undoubtedly be some tutorials on working with arrays on the web. I am using an excellent book: VBA and Macros for Microsoft Excel, by Jelen and Syrstad.
 
Upvote 0
I was hoping it would be closely related to and be nearly as simple as the formula. Something along the lines of:
Code:
Dim myArray As Variant

myArray = Array(1, 2, 3)

Range("D2").Value = Application.WorksheetFunction.ArraySum( _
    Application.WorksheetFunction.countifs(Range("I2:I37"), myArray))
I bought the book you referenced on kindle and after reading the array chapter, I guess that wouldn't be nearly as simple as the formula looked and I'd have to use a loop to individually call each data set in the array to sum it up.
Thanks for the recommendation though, this would help me a lot.

I'm pretty sure that I can do this now without the use of an array. However, lets say I want to make it run faster and store the entire data table into an array as you suggested. How would you manipulate ranges inside the table after loading it into the array? Let's say I load the table from F2:I37 into an array.
Code:
Dim myArray As Variant

myArray = Range("F2:I37")
How would I emulate =countif(I1:I37,1) using the 2D array? I can load array data individually, but have no idea how to use it as a range.
 
Upvote 0
here is a small example. You have to take care with arrays, because they are sneaky in that rows and columns seem to be reversed. So that is why i use the transpose to load the rang in the array. by the way
[B2:B11] is a short form for
Range("B2:B11")
Code:
Sub testar()    
    Dim arA As Variant
    
    arA = Application.WorksheetFunction.Transpose([B2:B11])
    
    MsgBox Application.WorksheetFunction.Sum(arA)


End Sub
What is always helpful working with arrays is to add the array to the 'Watch' window in the macro editor. Then you can see how the data are sitting in the array
 
Upvote 0
That makes it into a single dimension array.

But how would you selectively pick out a single column of data in the array if you had loaded an entire table, say A1:B11? I can't separately make multiple single dimension arrays and expect them to work together for setting multiple conditions inside a countifs() function, can I?
 
Upvote 0

Forum statistics

Threads
1,203,025
Messages
6,053,103
Members
444,639
Latest member
xRockox

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