Excel VBA Error 6 Overflow with sumifs and calculation (math)

EuginG

New Member
Joined
May 3, 2016
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I am trying to do some calculation with sumifs and criterion. I need to do SUMIFS of the first column (jRange) and to divide it by SUMIFS of the second column (lRange) several times (based on different criterion). And put a result to the certain cells.
I have found a solution for that (probably not the best, but it works). Just using / for dividing.
But.. It works only if I have necessary data for calculation in my Excel file. If a script based on my criterion cannot match the data then the whole script goes to Error 6 OVERFLOW.
Please help!

Sub test()

Dim Cell As Range, gRange As Range, lRange As Range, jRange As Range, mRange As Range

Set gRange = Range("G4:G5000")
Set lRange = Range("L4:L5000")
Set jRange = Range("J4:J5000")
Set mRange = Range("M4:M5000")

Range("S11").Value = Application. WorksheetFunction.SumIfs(jRange, gRange, 21, mRange, "OK", jRange, ">=10.5") / Application.SumIfs(lRange, gRange, 21, mRange, "OK", jRange, ">=10.5")
Range("T11").Value = Application. WorksheetFunction.SumIfs(jRange, gRange, 33, mRange, "OK", jRange, ">=16.5") / Application.SumIfs(lRange, gRange, 33, mRange, "OK", jRange, ">=16.5")

And so on..

End sub
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
In addition.

I understand, that the error above is because of 0 / 0 division. But I do not know how to avoid it. Maybe there are some tricks?
 
Last edited:
Upvote 0
Try...

Code:
Sub test()

    Dim Cell As Range, gRange As Range, lRange As Range, jRange As Range, mRange As Range
    
    Set gRange = Range("G4:G5000")
    Set lRange = Range("L4:L5000")
    Set jRange = Range("J4:J5000")
    Set mRange = Range("M4:M5000")
    
    
[COLOR=#ff0000]    Dim dblNumerator As Double
    Dim dblDivisor As Double
    
    dblNumerator = Application.SumIfs(jRange, gRange, 21, mRange, "OK", jRange, ">=10.5")
    dblDivisor = Application.SumIfs(lRange, gRange, 21, mRange, "OK", jRange, ">=10.5")
    
    If dblDivisor <> 0 Then
        Range("S11").Value = dblNumerator / dblDivisor
    End If[/COLOR]
    
    'etc
    '
    '
    '

End Sub

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,946
Members
449,198
Latest member
MhammadishaqKhan

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