VBA cannot find SUMIFS in WorksheetFunction class

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,940
Office Version
  1. 365
What's wrong here?

Code:
    Dim Arg1 As Range 'the range i want to sum
    Dim Arg2 As Range 'criteria range
    Dim Arg3 As Range 'the criteria (range)
    
    Set Arg1 = Sheets("TextFile").Range("M5:N10000")
    Set Arg2 = Sheets("TextFile").Range("K5:K10000")
    Set Arg3 = Sheets("TextFile").Range("H5:H10000")
    
' get the sum of the drive time hours
For i = 5 To 8
Sheets("TextFile").Cells(i, 21) = Application.WorksheetFunction.SumIfs(Arg1, Arg2, Cells(i, 20).Value, Arg3, "Drive Time")
Next i
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I suppose I have to separate the range for Arg1 into two separate ranges??
 
Upvote 0
What happens with

Code:
For i = 5 To 8
Sheets("TextFile").Cells(i, 21) = Application.WorksheetFunction.SumIfs(Arg2, Cells(i, 20).Value, Arg3, "Drive Time",Arg1)
Next i
 
Upvote 0
I haven't tried that, but don't think it will work because the 2nd element of the SUMIFS function needs to be a range which is used to match the third element.
 
Upvote 0
The sum is correct, but the range of the sum says "N" must be "M"

Set Arg1 = Sheets("TextFile").Range("M5:M10000")
 
Upvote 0
Sorry for that, now I understand that you have 2 columns.
It may be so:

Code:
Sub test()
  Dim Arg1 As Range 'the range i want to sum
  Dim Arg2 As Range 'criteria range
  Dim Arg3 As Range 'the criteria (range)
  
  Set Arg1 = Sheets("TextFile").Range("M5:M10000")
  Set Arg2 = Sheets("TextFile").Range("K5:K10000")
  Set Arg3 = Sheets("TextFile").Range("H5:H10000")
    
  ' get the sum of the drive time hours
  For i = 5 To 8
    Sheets("TextFile").Cells(i, 21) = Application.WorksheetFunction.SumIfs(Arg1, Arg2, Cells(i, 20).Value, Arg3, "Drive Time") + _
        Application.WorksheetFunction.SumIfs(Arg1.Offset(, 1), Arg2, Cells(i, 20).Value, Arg3, "Drive Time")
  Next i
End Sub
 
Upvote 0
Thanks. That's what I concluded in my post #2 and already incorporated it into the code. Thanks for confirming!!
 
Upvote 0
Well, with my example you don't need 2 ranges.
But I'm glad to know that you already solved it.
 
Upvote 0

Forum statistics

Threads
1,224,748
Messages
6,180,721
Members
452,995
Latest member
isldboy

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