How do I use a function in a SUMIF in VBA?

Drawleeh

New Member
Joined
Sep 2, 2021
Messages
34
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Hello, I have the following code which (should) create an array.

VBA Code:
Function UnitCheckArr()
Dim UnitValueArr(2 To 250) As Long
Dim UnitValue As Long


For UnitValue = LBound(UnitValueArr) To UBound(UnitValueArr)
UnitValueArr(UnitValue) = Cells(UnitValue, 4) * Cells(UnitValue, 6)
Next UnitValue

End Function

I want to use this array as part of a WorksheetFunction.SUMIF as the [RANGE SUM] for something like this:

VBA Code:
Sub NetSumIF()
If [COUNTA(F2:F250)=0] Then

Worksheets("Sheet1").Range("K2:K250") = Application.WorksheetFunction.SumIf(Worksheets("Sheet1").Range("I2:I250"), "I2:I250", Worksheets("Sheet1").Range("UnitCheckArr"))

End If
End Sub

But currently its not working and throwing up an application defined run time error. Any help in figuring this out would be much appreciated.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can only pass a Range to the [RANGE SUM] argument of sumif (there's a big clue in the tooltip).

As the array appears to be the product of columns D and F, you would be better off using sumproduct with D2:D250 and F2:F250 as ranges in the formula and dropping the array code.
 
Upvote 0
You can only pass a Range to the [RANGE SUM] argument of sumif (there's a big clue in the tooltip).

As the array appears to be the product of columns D and F, you would be better off using sumproduct with D2:D250 and F2:F250 as ranges in the formula and dropping the array code.
I was using the array as I didin't want to parse the result of those 2 columns into the worksheet itself and just hold them as an array in a function to be used later? Is there any other way I can hold that calculation as a range to be used in the sumif instead?
 
Upvote 0
Is there any other way I can hold that calculation as a range to be used in the sumif instead?
A range is exactly what is in the worksheet. Once you make any kind of change to it in vba (a calculation) it becomes an array.
I was using the array as I didin't want to parse the result of those 2 columns into the worksheet itself and just hold them as an array in a function to be used later?
If you use sumproduct instead of sumif then you don't need to do either, it will be done as a single process. Unless you're going to assign the array produced by the function to a variable to be used multiple times in the same procedure, it serves no purpose whatsoever.

What is the correct calculation required for K2 (as a single cell)? Perhaps if the requirement of the output was clearer then it would be possible to suggest a viable alternative.
 
Upvote 0
A range is exactly what is in the worksheet. Once you make any kind of change to it in vba (a calculation) it becomes an array.

If you use sumproduct instead of sumif then you don't need to do either, it will be done as a single process. Unless you're going to assign the array produced by the function to a variable to be used multiple times in the same procedure, it serves no purpose whatsoever.

What is the correct calculation required for K2 (as a single cell)? Perhaps if the requirement of the output was clearer then it would be possible to suggest a viable alternative.
Well I've been trying to adapt a worksheet that I had made with formulas into VBA as it's gotten very slow and I've been stumped on how to store output without putting it into the worksheet itself and then use it for further calculation.

I have column A and B which is concatenated to provide the range for my SUMIF. Then I multiply 2 ranges to get my [SUM RANGE] value for the sum-if. Finally I use the unique column that I get from an index match function (in vba) as my sumif criteria.

I've been trying to create a VBA that could do the same as the formulas without the need to show the "Concatenate" and the "Value" columns on the worksheet, thats why I have been trying to use array's to store that information first.

TESTING.xlsm
ABCDEFGHIJ
1CODEORIGINConcatenateQuantityUnit NetValueNet WeightGross WeightUNIQUENet Weight HS
296082000GB96082000 GB112022096082000 GB1220
332131000GB32131000 GB242048032131000 GB480
432139000JP32139000 JP202040032139000 JP1700
532139000GB32139000 GB412082032139000 GB820
632139000CN32139000 CN232046032139000 CN6460
749111090CN49111090 CN112022049111090 CN2120
849111090CN49111090 CN132026049111090 GB1320
949111090GB49111090 GB452090094036030 GB3580
1094036030GB94036030 GB5420108094036030 CN320
1194036030CN94036030 CN1420280
1294036030GB94036030 GB2020400
1394036030GB94036030 GB1220240
1494036030GB94036030 GB2020400
1532139000JP32139000 JP2120420
1632139000JP32139000 JP2020400
1749111090CN49111090 CN4120820
1849111090GB49111090 GB2120420
1994036030GB94036030 GB2020400
2094036030CN94036030 CN22040
2194036030GB94036030 GB2120420
2294036030GB94036030 GB2020400
2394036030GB94036030 GB1220240
2432139000JP32139000 JP1220240
2532139000JP32139000 JP1220240
2632139000CN32139000 CN300206000
2749111090CN49111090 CN2020400
2849111090CN49111090 CN2120420
2996082000GB96082000 GB50201000
Sheet1
Cell Formulas
RangeFormula
J2:J10J2=SUMIF(C$2:C$250,I2,F$2:F$250)
C2:C29C2=A2&" "&B2
F2:F29F2=D2*E2
 
Upvote 0
What else do you have in your worksheet?

The reason that I ask is because I think you are looking in the wrong place to improve performance. The formulas in columns C and E are too simple to cause any problems, even with large volumes of data. Even the sumif formula wouldn't cause slow processing unless you have in the region of 10,000 unique records in column I.

You could eliminate the need for concatenating by using SUMIFS instead of SUMIF but I doubt that it will make much difference, if any. I would be more inclined to keep the helper columns (C and E) then generate the output with a pivot table from those 2 columns instead of using formulas.

Things that can cause slowness are heavy use of conditional formatting, formulas (or named range definitions) that use volatile functions such as OFFSET, INDIRECT, TODAY and NOW (there are more but these are the most common culprits).
 
Upvote 0
What else do you have in your worksheet?

The reason that I ask is because I think you are looking in the wrong place to improve performance. The formulas in columns C and E are too simple to cause any problems, even with large volumes of data. Even the sumif formula wouldn't cause slow processing unless you have in the region of 10,000 unique records in column I.

You could eliminate the need for concatenating by using SUMIFS instead of SUMIF but I doubt that it will make much difference, if any. I would be more inclined to keep the helper columns (C and E) then generate the output with a pivot table from those 2 columns instead of using formulas.

Things that can cause slowness are heavy use of conditional formatting, formulas (or named range definitions) that use volatile functions such as OFFSET, INDIRECT, TODAY and NOW (there are more but these are the most common culprits).
Unfortunately that is only one snippet I have created for the purposes of explaining the above, the actual worksheet is very heavy with thousands of entries, and these helper columns are 2 of many that are used to create ranges for other formula to work from.
 
Upvote 0
Perhaps you'd be better off with a pivot table for summarising the data.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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