converting excel sumifs formula to vba code

22,796

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.

Share:
22,796
user2440270
Author by

user2440270

Updated on July 16, 2022

Comments

  • user2440270
    user2440270 almost 2 years

    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