Average of multiple columns

cjop0

New Member
Joined
Mar 7, 2018
Messages
2
I want to find a way of fixing this code so that the average can be calculated in the case where there is more than one column, but I have no idea how to do this. I'd really appreciate any solutions, thank you.



Function Aver(r As Range) As Double

Dim Sum As Double

If r.Columns.Count = 1 Then ' check that the range r has only one column
Sum = 0
For i = 1 To r.Rows.Count
Sum = Sum + r.Cells(i) ' we can use one index for Cells
' because range has only one column
Next i
Aver = Sum / r.Rows.Count ' function returns this value Aver if
' the range has only one column
Else
MsgBox "Data range has more than 1 column,”+vbNewLine+ “Average will be calculated only for the first column"
Sum = 0
For i = 1 To r.Rows.Count
Sum = Sum + r.Cells(i,1) '
Next i
Aver = Sum / r.Rows.Count' function returns this value Aver if
' the range has more than one column
End If

End Function
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You just want to calculate the average of some cells? Why not just use the built in AVERAGE function or have i missed something?
 
Upvote 0
cjop0,

Are you wanting to include all Columns that have data in them?
A B C D
2 4 6 8

Is it possible there would be any blank Columns with the data set?
A B C D
2 4 8

Is there a Header Row or does the data start on Row 1?


You could determine the last column and last row and then simply use the AVERAGE Function

For example if Column D is the last Column and Row 6 is the last Row in the code below
lc would be 4
lr would be 6

Code:
Dim lc As Long
Dim lr As Long
Dim aver As Long


lc = Cells(1, Columns.Count).End(xlToLeft).Column
lr = Cells(Rows.Count, "A").End(xlUp).Row
aver = WorksheetFunction.average(Range(Cells(1, 1), Cells(lr, lc)))

This works even if there are blank Columns or blank Rows
 
Upvote 0
The UDF in the OP takes the average of the cells in the first column of the range, unlike AVERAGE.

You could do something like

Code:
Function Aver(r As Range) As Double
    Aver = WorksheetFunction.Average(R.Columns(1))
End Function

or, instead of a formula like =Aver(someRange)

use native Excel =AVERAGE(INDEX(someRange,0,1))
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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