Link to home
Start Free TrialLog in
Avatar of bishop3000
bishop3000Flag for United States of America

asked on

VBA: Getting Range Averages w/o #DIV/0!

Hi,
In the VBE, I'm looping through a spreadsheet (that I can't modify) to get averages. Some ranges I'm getting averages for consist of all blank cells--so the result in those cases shows "#DIV/0!". What's the best way to make VBE give those ranges a "0" value, rather than a "#DIV/0!" value?

Here's the code format:

    Sub RangeAvg()
    Dim i as Integer, ShadeAvg as Integer, JanArray

    For i = 3 To 57 Step 2
        ShadeAvg = Application.Average(Range(Cells(16, i), Cells(46, i + 1)))
        JanArray((i - 1) / 2) = ShadeAvg
    Next i

    Range("d4:ae4").Value = JanArray()

   End Sub

Thanks!
-Michael
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Try:

    Sub RangeAvg()
    Dim i as Integer, ShadeAvg as Integer
    Dim JanArray(1 To 28) As Double

    For i = 3 To 57 Step 2
        ShadeAvg = Application.Average(Range(Cells(16, i), Cells(46, i + 1)))
        JanArray((i - 1) \ 2) = ShadeAvg
    Next i

    Range("d4:ae4").Value = JanArray

   End Sub

Kevin
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bishop3000

ASKER

Kevin,
You are the man.
Thank you!
-Michael