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