VBA Sumifs Sytanx Help

denzo36

Board Regular
Joined
Sep 22, 2019
Messages
218
Office Version
  1. 365
Platform
  1. Windows
Hi guys, new ish to VBA so would appreciate some help on the syntax for this, can't seem to find what I'm looking for elsewhere.

I would like to create a sumifs and countif formula in VBA that calculates a sum of premium and count of policy number.

I would like to create a 'Summary' sheet that basically looks at another sheet which has my data and does a sumifs based on 2 criteria and a countif based on one criteria (so I'm referencing a different sheet to the sheet my formulas will be in). Below is the formula in Excel:

=SUMIFS('All Key Risks'!D:D,'All Key Risks'!I:I,A2&"*")

where the name of the sheet I'm referencing is 'All Key Risks' and taking the sum range of column D with the criteria range being in column I and the criteria being the value in cell A2 of active sheet and adding a wildcard character.

I'm struggling to get the syntax right in my VBA code. Currently I have the below where B2 is the cell I want the formula populated in the 'Summary' sheet.

Range("B2").Value = application.WorksheetFunction.SumIfs(worksheets(1).range("D:D"),worksheets(1).range("I:I"),A2&"*"))

The problem I have is the correct way to reference the sheet name in my formula. The reason is used 'Worksheets(1) is because the data I'm summing from will always be on Sheet 1 so I'm trying to reference the range D:D and I:I on that sheet.

Anyone have a solution?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
How about
VBA Code:
Range("B2").Value = application.SumIfs(worksheets(1).range("D:D"),worksheets(1).range("I:I"),range("A2")&"*"))
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Hi Fluff - one more thing if possible. I've used your formula which works great but when I try and fill the formula down for all cells in the range its filling down the values of cells B2 instead of adjusting the sumifs formula for each cell column A (i.e. last argument in my Sumifs formula needs to move as I copy the formula down to look at A1, A2, A3 etc to end of that range)

Your previous tip: Range("B2").Value = Application.WorksheetFunction.SumIfs(Worksheets(1).Range("D:D"), Worksheets(1).Range("I:I"), Range("A2"))

Want to fill this formula to fill down for all cells in the range. My attempt: Range("B2").AutoFill Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)

Any ideas?
 
Upvote 0
Because you are inserting the result of the formula, not the formula itself, you cannot fill down.
You either need to put the formula in & then convert to values, or loop through the range calculating for each row
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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