VBA Using range variables for sumifs formula

Boffa

New Member
Joined
May 8, 2019
Messages
27
HI Guys need some help with this one - Done some searching but couldn't find anything that helps with my particular challenge here

The excel function I would to return via vba in Cell L3 is =SUMIFS(G3:G12,H3:H12,"Sales",I3:I12,"<>Excluded")/38

I want to achieve this using VBA and use variables to pass the ranges through to as the range sizes change

Below is my attempt but it does not seem to work

Any help would be greatly appreciated...

Dim LRow As Long
Dim SumRng As Range
Dim Criteria1Rng As Range
Dim Criteria2Rng As Range

LRow = Cells(Rows.count, 1).End(xlUp).row

Set SumRng = Range("G2", "G" & LRow)
Set Criteria1Rng = Range("H2", "H" & LRow)
Set Criteria2Rng = Range("I2", "I" & LRow)

Range("L3").Formula = "=SUMIFS(SumRng.address,Criteria1Rng.address,""Sales"",Criteria2Rng.address,""<>Excluded"")/38"
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You need to close the text string that makes up the formula and concatenate the vba ranges outside of the double quotes
VBA Code:
Range("L3").Formula = "=SUMIFS(" & SumRng.address & "," & Criteria1Rng.address & ",""Sales""," & Criteria2Rng.address & ",""<>Excluded"")/38"
hopefully I have them all in the right places.
 
Upvote 0
Bang on Jasonb75 ! Thank you, works a treat..

Going crazy trying to get those double quotes in the right places
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

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