Convert Excel SUM IF formula array to VBA code

Phitur

New Member
Joined
Jul 23, 2010
Messages
21
I need to pare down the number of calculations on the worksheet because it is slow. I'm trying to convert the following formula to a VBA code.

PHP:
{=SUM(IF(I:I>=AI3,IF(I:I<AI4,1,0),0))}
where,
col1 = "I"
col8 = "AI"
col11 = "AL"
i = 3
the current cell = AL3


Does anyone know how this could be represented in VBA to fill in the value for that cell instead of putting the equation in? I'm a bit lost on this one.

Thanks!


Reposting from http://www.mrexcel.com/forum/showthread.php?t=495161
[/COLOR]
 
Using SUMPRODUCT in VBA is probematic. Why not use

=SUMIF(I:I,"<"&A14,G:G)-SUMIF(I:I,"<"&A13,G:G) as your base formula and convert that.

Echoing Jonmo1's sentiments, how many cells are holding these formulas?
Its rare that substituting a UDF for sheet formulas saves time. Perhaps a Change event could be used to gain speed, but maintaining and editing the sheet in the future will be difficult, especially when the developer (you) is not super conversant in VBA.


There are about 2000 cells performing calculations. Of those at least half are using arrays. Once the sheet is setup, there is no need to return to it to manipulate the data. This code is intended to handle the analysis for final use. If the data does need to be referenced, it is still preserved in a usable format.

Per Fazza's comments, I think that would be a good approach to pursue (or an actual DB) for the next project. Something I'd need to research.

I converted the equation you suggested, but I get a type mismatch error. Here is the conversion I used.
Code:
Cells(i, col12).Value = Application.SumIf(Range(col1 & Chr(58) & col1), "<" & Filter(i + 1) _
  , "G:G") - Application.SumIf(Range(col1 & Chr(58) & col1), "<" & Filter(i), "G:G")

What would be causing the error?

Thanks
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If all the string variables resolve properly (e.g. col1 & Chr(58) & col1) the only thing that I can see is the "G:G" is not a range, but Range("G:G") is.

Try this syntax,
Application.SumIf(Range(col1 & Chr(58) & col1), "<" & Filter(i + 1), Range("G:G"))
 
Upvote 0
If all the string variables resolve properly (e.g. col1 & Chr(58) & col1) the only thing that I can see is the "G:G" is not a range, but Range("G:G") is.

Try this syntax,
Application.SumIf(Range(col1 & Chr(58) & col1), "<" & Filter(i + 1), Range("G:G"))

That's it! So many simple things. Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,214,531
Messages
6,120,073
Members
448,943
Latest member
sharmarick

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