VBA error on loop when no data to average

amphead

New Member
Joined
May 24, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

One of my coworkers created a VBA to average some data years ago. He has since retired and I need help correcting an error. I'm new to VBA.

I have a data point for every hour of the month. The VBA code is intended to calculate a 24 hour average. I've ran into an error due to an entire day of missing hour data, so no data to average for that day. The VBA gives me Run-time error "6": Overflow.

Can anyone provide me some direction getting the VBA code to move on to the next day if there is no data to calculate for the 24 hour average?

VBA Code:
' calculate 24 hour averages
For iday = 1 To itotdays
  istrt = (iday - 1) * 24 + 2
  istop = istrt + 23
  asum = 0
  n = 0
  For i = istrt To istop
    arange$ = "B" & i
    Set arang = Range(arange$)
    icnt = Application.WorksheetFunction.Count(arang)
    If icnt = 1 Then
      asum = asum + Cells(i, 2)
      n = n + 1
      asumtot = asumtot + Cells(i, 2)
      ntot = ntot + 1
    End If
    
  Next i
  aver = asum / n
  If n > 17 Then
    Cells(istop, 4) = aver
  End If
Next iday

When I click on debug, it highlights this line.
VBA Code:
aver = asum / n
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Welcome to the Board!

Hard to see/test without having your data, by maybe try this?
Rich (BB code):
For iday = 1 To itotdays
  istrt = (iday - 1) * 24 + 2
  istop = istrt + 23
  asum = 0
  n = 0
  For i = istrt To istop
    arange$ = "B" & i
    Set arang = Range(arange$)
    icnt = Application.WorksheetFunction.Count(arang)
    If icnt = 1 Then
      asum = asum + Cells(i, 2)
      n = n + 1
      asumtot = asumtot + Cells(i, 2)
      ntot = ntot + 1
    End If  
  Next i

  If n > 0 Then
    aver = asum / n
    If n > 17 Then
        Cells(istop, 4) = aver
    End If
  End If

Next iday
 
Upvote 0
Solution
Thanks for your help. I just added the extra If...Then and that corrected my problem.

Sorry about not adding data to the post. I'll try to remember to do that on the next one.
 
Upvote 0
You are welcome.
Glad my solution worked for you!
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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