averaging column data vba
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

replyReply Wed 2 Nov 2011, 15:12Delegate Peter said...

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

For upcoming training course dates see: Pricing & availability

replyReply Sat 5 Nov 2011, 20:07Trainer Simon said...

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

 

Please browse our web site to find out more about
microsoft.excel.courses.in.london and other Microsoft training courses.

Excel tip:

Hiding a worksheet in Excel

Want to circulate an Excel workbook file but keep certain worksheets hidden from your colleagues' view?

You can do so by bringing up the sheet you wish to hide on your screen; then going to Format - Sheet - Hide.

It will not be immediately obvious that a sheet is hidden from view unless perhaps the sheet are still labelled Sheet 1, Sheet 2 etc.

To display the sheet again, you can go to Format - Sheet - Unhide on any of the other sheets in the workbook. A dialogue box will appear, allowing you to select the hidden sheet/s. Click OK to make the sheet/s reappear again.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard