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...
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try declaring Arg3 and Arg5 as Ranges, then try Arg3.Value in the formula part.

Also, it would be better to declare Long instead of Integer (which is limited to a max value of 32767).
 
Upvote 0
Try declaring Arg3 and Arg5 as Ranges, then try Arg3.Value in the formula part.

Also, it would be better to declare Long instead of Integer (which is limited to a max value of 32767).
hmmm. That didn't do it. I did change Integer to Long thou. Thanks.
 
Upvote 0
Try declaring Arg3 and Arg5 as Ranges, then try Arg3.Value in the formula part.

Also, it would be better to declare Long instead of Integer (which is limited to a max value of 32767).
So, yesterday it calculated a value which was correct. It summed all numbers in column M of source sheet that is marked as "Completed" in Column D and matches same ID number in Column A. Today it returns a zero when the result obviously shouldn't be zero.
 
Upvote 0
I can see no reason for the results to be different on a daily basis, there must be something in the data that is causing a slight discrepancy. Quite possibly a formatting error of some kind, or even extra hidden characters if you copy and paste from another application.

Have you tried entering a formula into the sheet for a criteria set where the vba version is giving incorrect results in order to compare them?
 
Upvote 0
I agree with Jason, the code looks fine for me and works fine on my mocked up data.
Your code produces a formula like this which is the H2 version of the SumIfs on my mocked up data.
Excel Formula:
=sumifs('Data History'!$M$2:$M$9,'Data History'!$A$2:$A$9,$H$2,'Data History'!$G$2:$G$9,$S$1)

Result / Output

20210308 VBA using SumIfs with Multiple Criteria.xlsm
ABCDEFGHIJKLMNOPQRS
1Output of Sum If Col 6IDs to UseStatus to look for --->Complete
21030103
30104
40105
51060106
60107
7
Weekly Data


Test Input Data

20210308 VBA using SumIfs with Multiple Criteria.xlsm
ABCDEFGHIJKLM
1IDStatusAmt
2100Complete1000
3101Open1010
4102Complete1020
5103Complete1030
6104Open1040
7105Pending1050
8106Complete1060
9107Pending1070
10
Data History
 
Upvote 0
I can see no reason for the results to be different on a daily basis, there must be something in the data that is causing a slight discrepancy. Quite possibly a formatting error of some kind, or even extra hidden characters if you copy and paste from another application.

Have you tried entering a formula into the sheet for a criteria set where the vba version is giving incorrect results in order to compare them?
Thank you,

so rereading my original post, I wasn't clear. Let me try to clarify, the results are not different (yes, I compared the data entering the sumifs formula with the one produced using VBA) but on some occasions, the result calcuated Zero instead of an amount. This morning, I tried running the code again without making any changes and those 3 lines returned a number (changes from zero to a calculated amount same as "Sumif" formula). I'm baffled why of 26 lines, most of the cells calculate fine but in last 3 days, on 3 cells I get calculated number, a zero, and then calculated number. I'm running a decent HP Spectre laptop with Office 365 and don't see it being an issue with outdated Hardware (if that can make a difference) or software.
 
Upvote 0
I agree with Jason, the code looks fine for me and works fine on my mocked up data.
Your code produces a formula like this which is the H2 version of the SumIfs on my mocked up data.
Excel Formula:
=sumifs('Data History'!$M$2:$M$9,'Data History'!$A$2:$A$9,$H$2,'Data History'!$G$2:$G$9,$S$1)

Result / Output

20210308 VBA using SumIfs with Multiple Criteria.xlsm
ABCDEFGHIJKLMNOPQRS
1Output of Sum If Col 6IDs to UseStatus to look for --->Complete
21030103
30104
40105
51060106
60107
7
Weekly Data


Test Input Data

20210308 VBA using SumIfs with Multiple Criteria.xlsm
ABCDEFGHIJKLM
1IDStatusAmt
2100Complete1000
3101Open1010
4102Complete1020
5103Complete1030
6104Open1040
7105Pending1050
8106Complete1060
9107Pending1070
10
Data History
Thanks for testing. Yeah, I'm baffled why sometimes it returns zero instead of the proper sumif value.
 
Upvote 0
What would be best way to reference the value "Complete" within the VBA code instead of cell "S1"?
 
Upvote 0
What would be best way to reference the value "Complete" within the VBA code instead of cell "S1"?
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.
 
Upvote 0
Solution

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