WorksheetFunction SumIfs with multiple arguments doesn't return consistent results

marimar02

Board Regular
Joined
May 21, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hello,

I've been trying to find a solution before resorting to asking the community. I am learning VBA without selecting/activating cells (when not required) and without using excel formulas (unless absolutely necessary) and would like this to be a WorksheetFunction rather than a "FormulaR1C1".

This VBA code yesterday gave me different results than it did today and it has to do with Arguments 4 & 5 in which latter is simply a value "Complete". I added these arguments after testing it without. But I want the code to sum a range where results are marked as completed, too.

VBA Code:
Sub Populate_WL_Table()

Dim sSht As Worksheet, tSht As Worksheet
Dim sRng As Range, sRng2 As Range, tRng As Range
Dim sLastRow As Long, tLastRow As Long
Dim i As Integer, x As Integer
Dim Arg1 As Range, Arg2 As Range, Arg3 As Variant, Arg4 As Range, Arg5 As Variant

's = source
't = target

Set sSht = Sheets("Data History")
Set tSht = Sheets("Weekly Data")

sSht.Activate

sLastRow = sSht.Cells(sSht.Rows.Count, "G").End(xlUp).Row

tSht.Activate

tLastRow = tSht.Cells(tSht.Rows.Count, "H").End(xlUp).Row

Set Arg1 = sSht.Range("M2:M" & sLastRow) 'Sum Range
Set Arg2 = sSht.Range("A2:A" & sLastRow) '1st Argument Range
Set Arg4 = sSht.Range("G2:G" & sLastRow) '2nd Argument Range
Set Arg5 = tSht.Range("S1") '2nd Argument Criteria which is word "Complete"

    For i = 2 To tLastRow
        If tSht.Range("H" & i) <> "" Then 'skips rows where Range in H Column doesn't contain a value
            Set Arg3 = tSht.Range("H" & i) '1st Argument Criteria
            tSht.Cells(i, 6).Value = Application.WorksheetFunction.SumIfs(Arg1, Arg2, Arg3, Arg4, Arg5.Value)
        End If
    Next i

Set sSht = Nothing
Set tSht = Nothing
Set sRng = Nothing
Set tRng = Nothing

End Sub

Thank you and please let me know if I can clarify anything...
 
Just type it into the code as "Complete" in place of Arg5.Value (or whichever Arg it was).

In some cases, vba is case sensitive but I don't think that will be the problem you are encountering.

Hardware / software wouldn't make a difference unless you have a corrupt installation, but if that was the case you would have bigger problems than the occasion incorrect sumifs result.

I would highly suspect that the cause of the anomaly is the data that you are working with more than anything else.

The use of RAND() or NOW() in formulas could be one possible cause.
Thank you. I'll change the code to "Complete" instead of cell reference. I'll keep monitoring the issue and if it persists, I'll have to go back to R1C1 formulas. Thanks again. (y)
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

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