VBA for calculating Average

Peter04

New Member
Joined
Jun 4, 2019
Messages
3
Hi,

I have to calculate the average of each column values at sheet 2 and store it to sheet 3. While I am running the code, it ends with Runtime Error 6 Overflow. My code is given below.
Code:
Sub AvgCal()
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim count As Integer
Dim sum As Long
Dim avr As Long
Dim lstCol As Integer

lstCol = ws2.UsedRange.Columns(ws2.UsedRange.Columns.count).Column
For c = 2 To lstCol
    sum = 0
    count = 0
    ws2.Cells(1, c).Select
    Do While ActiveCell.Value <> ""
        sum = sum + ActiveCell.Value
        count = count + 1
        ActiveCell.Offset(1, 0).Activate
    Loop
     ws3.Cells(2, c).Value = sum / count 
Next c

End Sub

Could you please help me to settle this.. Thanks
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the forum.

Since you haven't mentioned which line causes the error, I'd start by changing the Integer declarations to Long, and change the sum variable to a Double.
 
Upvote 0
Hi Rory,
Thanks for your reply.
The line "ws3.Cells(2, c).Value = sum / count" causes the error.
Even after change the 'sum' variable to 'double', still the same error is coming.
Thanks
 
Upvote 0
This can happen if sum = 0 and count = 0.
Use this modified code line:
If count > 0 Then ws3.Cells(2, c).Value = sum / count

or better:
Rich (BB code):
    If count > 0 Then
      ws3.Cells(2, c).Value = sum / count
    Else
      ws3.Cells(2, c).ClearContents
    End If
 
Last edited:
Upvote 0
This is faster alterrnative code:
Rich (BB code):
Sub AvgCal1()
  Dim LastCol As Long, LastRow As Long
  Dim rng As Range
  With Range(ws2.Range("A1"), ws2.UsedRange)
    LastRow = .Rows.Count
    LastCol = .Columns.Count
  End With
  Set rng = ws3.Range("B2").Resize(1, LastCol - 1)
  With rng
    .Formula = "=IFERROR(AVERAGE('" & ws2.Name & "'!B1:B" & LastRow & "),"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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