Application.WorksheetFunction.SumIf: a range as a criteria

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm struggling with a variant of sumif.

Below a classic example:

Code:
Result = Application.WorksheetFunction.SumIf(Range("A:A"), Range("C1").Value, Range("B:B"))

Now, suppose the criteria is not univocal.

I mean: the criteria is not in C1, but has to correspond in one of the value included in a range.

For example, in the range rng

Code:
dim lr as long
lr = Cells(Rows.Count, "C").End(xlUp).Row
set rng as range
rng = Range("C2:C" & lr)

How can I figure this out?

Thank's in advance.
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello everybody.

I'm struggling with a variant of sumif.

Below a classic example:

Code:
Result = Application.WorksheetFunction.SumIf(Range("A:A"), Range("C1").Value, Range("B:B"))

Now, suppose the criteria is not univocal.

I mean: the criteria is not in C1, but has to correspond in one of the value included in a range.

For example, in the range rng

Code:
dim lr as long
lr = Cells(Rows.Count, "C").End(xlUp).Row
set rng as range
rng = Range("C2:C" & lr)

How can I figure this out?

Thank's in advance.

This way is impossible:

Code:
Result = Application.WorksheetFunction.SumIf(Range("A:A"), Range("C2").Value, Range("B:B")) +  _
 Application.WorksheetFunction.SumIf(Range("A:A"), Range("C3").Value, Range("B:B")) [COLOR="#FF0000"][B]+ ... + _
 [/B][/COLOR] Application.WorksheetFunction.SumIf(Range("A:A"), Range("C[COLOR="#FF0000"][B]?[/B][/COLOR]").Value, Range("B:B"))
 
Last edited:
Upvote 0
You could loop.
Code:
Dim i as Long, lr as Long, mySum as Long

lr = Cells(Rows.Count, "C").End(xlUp).Row

For i = 1 to lr
    mySum = mySum = Application.WorksheetFunction.SumIf(Range("A:A"), Cells(i, 3).Value, Range("B:B"))
Next i
 
Upvote 0
You could loop.
Code:
Dim i as Long, lr as Long, mySum as Long

lr = Cells(Rows.Count, "C").End(xlUp).Row

For i = 1 to lr
    mySum = mySum = Application.WorksheetFunction.SumIf(Range("A:A"), Cells(i, 3).Value, Range("B:B"))
Next i

It works fine. Thank you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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