Sumif

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
On Sheet1, I have the following 10 rows of data with its heading:

Rich (BB code):
Age     Metric
26-30  1
26-30  1
26-30  1
26-30  1
26-30 1 26-30 1 26-30 1 26-30 1 26-30 1 26-30 1


I don't understand why after the first iteration in the loop, ModelArray below returns a value of 9 instead of 10?

Rich (BB code):
Rich (BB code):
    Dim Rng As Range
    
    Set Rng = Sheet1.Range("$B$2:$B$11")
    
    Dim ModelArray() As Variant
    
    ModelArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
    
    Dim Counter As Integer
    
    For Counter = 2 To 11
    
        ModelArray(Counter, 2) = Application.WorksheetFunction.SumIf(Sheet1.Range("A:A"), "=" & ModelArray(Counter, 1), Rng)
     
    Next Counter ' PUT A BREAK POINT HERE

yet it appears to return the correct result if I changed the range to:

Rich (BB code):
Set Rng = Sheet1.Range("B:B")

Why is that?

Thanks
 
Last edited:

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.
you only have 10 rows to start with ?

I have 11 rows, of which rows 2 to 11 are the data and row 1 is the heading.

Therefore I thought if I set the range to lookup to be from B2 to B11, it would work but it doesn't.
 
Upvote 0
Your sum range is offset by one row from your criteria range.
 
Upvote 0
Your sum range is offset by one row from your criteria range.

Isn't this line saying in plain English:

Rich (BB code):
ModelArray(Counter, 2) = Application.WorksheetFunction.SumIf(Sheet1.Range("A:A"), "=" & ModelArray(Counter, 1), Rng)

Sum everything in column A, when it's value is equal to "26-30" and actually sum everything in Rng, where Rng is the range B2 to B11?

If I am correct, then Rng contains the value of 1 (10 times), hence I expected it to return 10, not 9?
 
Last edited:
Upvote 0
Because of the offset, B2 is added if A1 contains the value of interest, then B3 if A2 has it, and so on.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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