Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

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

ResolvedVersion 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:

Freeze Rows and Columns to keep lables displayed

You can freeze rows and columns in your worksheet so they don't move.

This allows you to keep row and column labels displayed on your screen as you move through a large worksheet.

Click below and/or to the right of the cell(s) you want to freeze. (NB. Excel freezes ALL the rows above and ALL the columns to left of the selected cell)

Click on the 'Windows' menu and selct 'Freeze Panes'.

Lines appear in your worksheet. The required rows and columns are frozen and remain on your screen as you move through your worksheet.

To unfreeze rows and columns, click on 'Window' menu and select 'Unfreeze Panes'.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.