averaging column data vba

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Averaging column data in VBA

Averaging column data in VBA

resolvedResolved · Medium Priority · Version 2010

Peter has attended:
Excel VBA Intro Intermediate course

Averaging column data in VBA

Hi, I was hoping you could help me find a nicer way of averaging the data in a column of unknown length. Just telling the cell exactly what it should contain does work but is not very flexible and i'm sure there is a nicer way of doing it. This does work because excel doesnt count cells which are empty for the averaging divide.


Sheets(SheetName).Range("e5").Value = "=AVERAGE(e12:e1048576)"

Thanks,

Peter

RE: Averaging column data in VBA

Hi Peter,

Thank you for your question and welcome to the forum.

Try this code:


Sub UseExcelFunctions()

Dim iColumnCount As Integer
Dim lRowCount As Long
Dim iCol As Integer
Dim dAverage As Double


Sheets("Sheet2").Activate

iColumnCount = ActiveSheet.UsedRange.Columns.Count
lRowCount = ActiveSheet.UsedRange.Rows.Count

For iCol = 1 To iColumnCount

dAverage = WorksheetFunction.Average(Columns(iCol))

Cells(lRowCount + 2, iCol).Value = dAverage

Next iCol

End Sub

I hope this helps.

Regards

Simon

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Apply Autosum with keyboard shortcut

If you press Alt and = at the same time, it applies autosum.

View all Excel hints and tips


Server loaded in 0.08 secs.