View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone[_2_] Jim Cone[_2_] is offline
external usenet poster
 
Posts: 1,549
Default recalculating RAND() inside a VBA loop

Hi Dave,
I called the function from a sub.
The ActiveCell had "=RAND()" in it...
'---
Sub huh()
MsgBox expval(ActiveCell, 5)
End Sub
'---
Jim Cone
Portland, Oregon USA


"Dave Peterson"
wrote in message
About the only thing that a function called from a worksheet cell can do is
return a value to the cell with the function.
Application.calculate fails for me (xl2003).
Maybe you could use VBA's Rnd function instead of looking back at the
worksheet???

Option Explicit
Function expval(Optional iter As Long) As Double
Dim arr() As Double
Dim loops As Integer
Dim X As Long
Randomize
'set itterations
loops = Application.WorksheetFunction.Max(iter, 20)
'set array
ReDim arr(1 To loops)
'loop through value measurements
For X = 1 To loops
arr(X) = Rnd
Next X
'compute average value
expval = Application.WorksheetFunction.Average(arr)
End Function
Dave Peterson