Dynamic Arrays?

shittytrader

New Member
Joined
Apr 9, 2011
Messages
1
I got this example from http://www.anthony-vba.kefra.com/vb...102.htm#Random_Number_and_Randomize_Statement and was having some trouble understanding some stuff. appreciate any help i can get.

my question relates to the sub procedure. The array has been declared with (10). I was wondering how would you make this dynamic. So that instead of stopping at 10 rows, the sub procedure would calculate all the consecutive rows with numbers in it.

thanks.





<small style="color: rgb(51, 51, 255); font-weight: bold;"></small><small style="color: rgb(51, 51, 255); font-weight: bold;">Function Mean(Arr() As Single)</small>
<small style="color: rgb(51, 51, 255);"> Dim Sum As Single</small>
<small style="color: rgb(51, 51, 255);"> Dim i As Integer

</small><small style="color: rgb(51, 51, 255);"> Sum = 0</small>
<small style="color: rgb(51, 51, 255);"> For i = 1 To UBound(Arr)</small>
<small style="color: rgb(51, 51, 255);"> Sum = Sum + Arr(i)</small>
<small style="color: rgb(51, 51, 255);"> Next i</small>
<small style="color: rgb(51, 51, 255);"> </small>
<small style="color: rgb(51, 51, 255);"> Mean = Sum / UBound(Arr)</small><small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255); font-weight: bold;">End Function</small>
<small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255); font-weight: bold;">Function StdDev(Arr() As Single)</small>
<small style="color: rgb(51, 51, 255);"> Dim i As Integer</small>
<small style="color: rgb(51, 51, 255);"> Dim avg As Single, SumSq As Single</small>
<small style="color: rgb(51, 51, 255);"> </small>
<small style="color: rgb(51, 51, 255);"> avg = Mean(Arr)</small>
<small style="color: rgb(51, 51, 255);"> For i = 1 To UBound(Arr)</small>
<small style="color: rgb(51, 51, 255);"> SumSq = SumSq + (Arr(i) - avg) ^ 2</small>
<small style="color: rgb(51, 51, 255);"> Next i</small>
<small style="color: rgb(51, 51, 255);"> </small>
<small style="color: rgb(51, 51, 255);"> StdDev = Sqr(SumSq / (UBound(Arr) - 1))</small><small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255); font-weight: bold;">End Function</small>
<small style="color: rgb(51, 51, 255);"></small>

<small>The following sub routine reads the data in column one from row 1 to 10 (of Sheet1) into the array, calls both functions by passing the arguements to them, computes the mean (average) and the standard deviation, then returns the values in a message box.</small>

<small style="color: rgb(51, 51, 255);">Sub compute()</small>
<small style="color: rgb(51, 51, 255);"> Dim Arr(10) As Single</small>
<small style="color: rgb(51, 51, 255);"> Dim Average As Single</small>
<small style="color: rgb(51, 51, 255);"> Dim Std_Dev As Single</small>
<small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255);"> For i = 1 To UBound(Arr)</small>
<small style="color: rgb(51, 51, 255);"> Arr(i) = Sheets("Sheet1").Cells(i, 1)</small>
<small style="color: rgb(51, 51, 255);"> Next i</small>
<small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255);"> Average = Mean(Arr)</small>
<small style="color: rgb(51, 51, 255);"> Std_Dev = StdDev(Arr)</small><small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255);"> MsgBox "Average:" & vbTab & Average & vbCrLf & "StdDev :" & vbTab & Std_Dev</small><small style="color: rgb(51, 51, 255);"> </small><small style="color: rgb(51, 51, 255);"></small>
<small style="color: rgb(51, 51, 255);">End Sub</small>
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,224,574
Messages
6,179,628
Members
452,933
Latest member
patv

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