VBA SUMIF on Array

Silverjman

Board Regular
Joined
Mar 19, 2014
Messages
110
OK, so I am wanting to do a sumif on a column on an array because I don't want to print to the worksheet in order to obtain a range for a Worksheet.function.Sumif

The first column total is total of 197,321,164 is correct, the next columns are low and instead of going to quarter 40 the Else kicks in and everything after 8 is 0. The first "To_Quarter" in the array is 9 so with the >= I would think it would go to 9. I tried putting my Next I before the End IF but then it just asks for the For.

Any help would be much appreciated.

VBA Code:
Dim MaxRecov_If_result, arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter, arr_Row10_Resolution__Max_Recovery_Grid As Variant

arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter = Range("_Row10_Resolution_Physical_Possession_Expenses_To_Quarter")
arr_Row10_Resolution__Max_Recovery_Grid = Range("_Row10_Resolution__Max_Recovery_Grid")

ReDim arrIf_Max_Recovery_Amount_Sum(1 To 1, 1 To UBound(arrQuarters, 2))
        For J = LBound(arrQuarters, 2) To UBound(arrQuarters, 2)
        For I = LBound(arrNumber_of_Assets, 1) To 1 + UBound(arrNumber_of_Assets, 1)
            
   If arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter(I, 1) >= arrQuarters(1, J) Then
                MaxRecov_If_result = MaxRecov_If_result + arr_Row10_Resolution__Max_Recovery_Grid(I, J)
                    Else: MaxRecov_If_result = 0
                    End If
                Next I
          arrIf_Max_Recovery_Amount_Sum(1, J) = MaxRecov_If_result
     MaxRecov_If_result = 0
       Next J

Annotation 2020-01-27 194726.png
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
because I don't want to print to the worksheet in order to obtain a range for a Worksheet.function.Sumif

I don't understand that reasoning. Can you explain in words what you would like to calculate? If it can be done with Sumif in a worksheet, it can be done with Sumif in VBA.
 
Upvote 0
I'm under the impression that Sumif demands a Range type, whereas I have an array that is just sitting in VBA. I don't know how to convert an array to a range other than by printing it to the worksheet and then sucking that back up to VBA becasue I am trying to optimize for speed.

I thought a If Sum loop would be fairly simple...
 
Upvote 0
You can put a sample of your data and the expected results.

Use XL2BB tool
 
Upvote 0
Your code resets the variable to 0 when just ONE item in the array does not meet the condition. You may want to change that to do just nothing, so the variable retains the value it has and on the next loop iteration the next value can be added to the previous one.

If you want a different result, don't set the variable to zero.

Also, indent your code properly, so it's easier to find where things are at.

Does that make sense?
 
Upvote 0
Certainly found that WorksheetFunction.Sum(1DArray) did work.

No, looking at a code snip of something I'm running WorksheetFunction.Sum(Application.Index(2DArray, , 2)) works for me too.

Some go where none have tried to go before.
 
Upvote 0
The OP is doing a SumIF, not sum
 
Upvote 0
Cool, OK so I don't know if it picked up my named ranges so I've color coded them. I made a separate sample workbook and the code works correctly until quarter 11 (red).


VBA Code:
Option Explicit

Sub SumifONarray()
Dim arrQuarters, arrNumber_of_Assets As Variant

Dim I As Long, J As Long

arrNumber_of_Assets = Range("Costs_Number_of_Assets")
arrQuarters = Range("Quarters_1to40")

Dim MaxRecov_If_result, arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter, arr_Row10_Resolution__Max_Recovery_Grid As Variant

arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter = Range("_Row10_Resolution_Physical_Possession_Expenses_To_Quarter")
arr_Row10_Resolution__Max_Recovery_Grid = Range("_Row10_Resolution__Max_Recovery_Grid")

ReDim arrIf_Max_Recovery_Amount_Sum(1 To 1, 1 To UBound(arrQuarters, 2))
        For J = LBound(arrQuarters, 2) To UBound(arrQuarters, 2)
        For I = LBound(arrNumber_of_Assets, 1) To UBound(arrNumber_of_Assets, 1)
            
   If arr_Row10_Resolution_Physical_Possession_Expenses_To_Quarter(I, 1) >= arrQuarters(1, J) Then
                MaxRecov_If_result = MaxRecov_If_result + arr_Row10_Resolution__Max_Recovery_Grid(I, J)
                    Else: MaxRecov_If_result = 0
                    End If
                Next I
                    arrIf_Max_Recovery_Amount_Sum(1, J) = MaxRecov_If_result
                    MaxRecov_If_result = 0
                Next J
End Sub
28.1.20 sumif.png

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQAR
7This row is what I am try to find with "arrIf_Max_Recovery_Amount_Sum" in the VBA code 496,129 496,129 496,129 496,129 496,129 496,129 496,129 496,129 496,129 422,803 376,567 376,567 376,567 329,622 329,622 182,375 182,375 90,437 - - - - - - - - - - - - - - - - - - - - - -
8Quarters_1to40
9Costs_Number_of_Assets_Row10_Resolution_Physical_Possession_Expenses_To_Quarter12345678910111213141516171819202122232425262728293031323334353637383940
1019_Row10_Resolution__Max_Recovery_Grid73,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,32773,327
1121842,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,91942,919
1231550,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,17450,174
1341346,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,94546,945
1451747,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,95147,951
1561847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,51847,518
1671546,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,236
1781046,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,23646,236
1891550,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,83750,837
19101743,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,98643,986
Sheet1
Cell Formulas
RangeFormula
E7:AR7E7=SUMIFS(E$10:E$19,_Row10_Resolution_Physical_Possession_Expenses_To_Quarter,">="&E$9)
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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