Sumproduct with criteria in VBA?

SabCie

New Member
Joined
Dec 20, 2010
Messages
24
Hi,

I would like to make a sumproduct with a criteria while only using VBA, Is this something possible?

So to make this clearer, I have a document with 3 columns.

Column A = Category
Column B = Amount
Column C = Quantity

So I would like a VBA function that will give me the total amount (Amount*Quantity) for a specific category...

Help is really appreciated.

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi, you have to use Evaluate to run the worksheet function. See this example:

Excel Workbook
ABCDEF
1CategoryAmountQuantityCheck columnCheck result
2Food5.02420.08CategoryFood
3Drink19.928159.36Sum675.62
4Dancing19.4615291.9
5Food23.255116.25
6Drink8.94762.58
7Dancing18.268146.08
8Food25.2516404
9Drink5.511055.1
10Dancing20.3211223.52
11Food23.15369.45
12Drink12.8710128.7
13Dancing17.628140.96
14Food8.23865.84
Sheet24



Well, this VBA displays the same result ( 675.62 ), by using SUMPRODUCT:
Code:
Sub doSUMPinsheet()
myres = Evaluate("SUMPRODUCT(--(A2:A14= ""Food"")*(B2:B14)*(C2:C14))")
MsgBox myres
End Sub
 
Upvote 0
By the way, you could coerce your text test into numbers in an array first, and then use sumproduct normally, like this:
Code:
Sub doSUMPinsheetCoerceTextTest()

myarray = Evaluate("=--(A2:A14=""Food"")")

myres = Application.SumProduct(myarray, Range("B2:B14"), Range("C2:C14"))

MsgBox myres
End Sub
 
Upvote 0
hi!

It's possible to put a variable in this code?

myrangeisvariable="A2:A14"
mynameisvariable="FOOD"

myarray = Evaluate("=--(myrangeisvariable=FOOD)")

I want to know how the right code for that.

Thanks for your help and sorry for my horrible english :-(
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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