Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting excel sumifs formula to vba code

I'm trying to do a SUMIFS calculation in VBA. It works fine when I enter it on the spreadsheet, but when I try to convert it to VBA, it doesn't seem to work.

Sheets("Master").Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
    "=SUMIFS(Input!C32,Input!C37,Master!C1,Input!C31,Master!R1C)"

This is the snippet of code (originally in a comment):

Dim LastRow As Long 
Dim rw As Long 
LastRow = Range("A" & Rows.Count).End(xlUp).Row 
For rw = 2 To LastRow 
  Sheets("Master").Cells(rw, 2).Value = Application.WorksheetFunction.SumIfs(Sheets("Input").Range("AF:AF"), Sheets("Input").Range("AK:AK"), Sheets("Master").Range("A:A"), Sheets("Input").Range("AE:AE").Sheets("Master").Range("B2"))  
Next 
like image 648
user2440270 Avatar asked Feb 27 '26 03:02

user2440270


1 Answers

You aren't specifying the values you want to lookup in your criteria1. You have to specify a value, not a range.

Your sum range is fine.

Sheets("Input").Range("AF:AF")

Your criteria range1 is fine.

Sheets("Input").Range("AK:AK")

Your criteria1 needs to be a value, not a range.

Use this Sheets("Master").Range("A2").value instead of Sheets("Master").Range("A:A")

Obviously you can replace the 2 in the criteria1 with a variable if you need to to get your loop to work.

like image 162
Jon Crowell Avatar answered Mar 02 '26 09:03

Jon Crowell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!