# while loop for calculating average in vba code

#### maakali

##### New Member
hi,

i would like to take average of a coloum. i tried using for loop but it restricts the number of rows(as i had to define an array). i want xldown command with while loop so that last cell in a coloumn is used. can anyone help please. i am not sure how while code would work as i want to develop the code futher to calulate st deviation etc but if i get the concept right i will be able to develop

thanks

### Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

#### VoG

##### Legend
I'm not sure that you need to loop. Try

Code:
``````Sub sums()
Dim x As Double
x = WorksheetFunction.Average(Columns("A"))
MsgBox x
End Sub``````

#### maakali

##### New Member
hi buddy,

i think i will need a loop as you can see from this code. how will i calculate average from every row . this code works fine in for loop but i dont know how to do with while or do loop

Function stdDev(arr() As Single)
Dim i As Integer
Dim avg As Single, SumSq As Double, VAR As Single
avg = MEAN(arr)
For i = 1 To UBound(arr)
arr(i) = Sheets("sheet1").Cells(i, 1)
SumSq = SumSq + ((arr(i) - avg) ^ 2)

Next i
stdDev = Sqr(SumSq / (UBound(arr) - 1))
End Function

Replies
8
Views
364
Replies
2
Views
168
Replies
5
Views
169
Replies
1
Views
142
Replies
4
Views
676

1,190,878
Messages
5,983,366
Members
439,841
Latest member
goodwillhunting

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

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