VBA SUMIFS formula with Dynamic range

GRCArizona

Board Regular
Joined
Apr 24, 2010
Messages
95
Hi All -

I'm using a UF where a user can select from 1 to 5 Categories. I'm looking for a formula in VBA that uses a SUMIFS formula where the Category is dynamic. So, if a user selects only 1 category IE: Tires, the SUMIFS formula would be:

SUMIFS(F$5:F$5282,$C$5:$C$5282,"Tires",$D$5:$D$5282,$D5290

If the user selects "Tires" and "Battery", the formula would be:

SUMIFS(F$5:F$5282,$C$5:$C$5282,"Tires",$D$5:$D$5282,$D5290 + SUMIFS(F$5:F$5282,$C$5:$C$5282,"Battery",$D$5:$D$5282,$D5290.

Here is the code I'm using:
Dim n as Integer
Dim Category as String

Category = "="

For n = 1 To IncludedList.ListCount
IncludedList.SetFocus
IncludedList.ListIndex = n - 1

Category = Category & "+" & "SUMIFS(F$5:F$5282,$C$5:$C$5282," & IncludedList.Value & ",$D$5:$D$5282,$D5290)"

Next

I've got the code working with the exception of the dynamic part. The formula returned is not putting quotes around Tires, so the formula is not summing up b/c the quotes are missing. If I add quotes to the VBA, then instead of returnign "Tires", I'm getting & IncludedList.Value &

Anybody know what I can use to ensure this works?

Thx
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,226,693
Messages
6,192,465
Members
453,725
Latest member
cvsdatreas

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