Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default recalculating RAND() inside a VBA loop

How do I get cells in spreadsheet to recalculate new values using RAND() when
looping inside VBA? I'm trying to compute the average value of a cell who's
dependants contain values generated from the RAND() function. Sample code is
below.

Many thanks for any insights.


Function expval(target As Range, Optional iter As Integer) As Double

'measure value
'record in an array
'recompute value
'repeat
'compute average value

Dim rowLocation As Integer
Dim colLocation As Integer
Dim arr() As Double
Dim loops As Integer
Dim X As Integer


'determine cell location
rowLocation = target.Row
colLocation = target.Column


'set itterations
loops = Application.WorksheetFunction.Max(iter, 20)

'set array
ReDim arr(loops)

'loop through value measurements
For X = 1 To loops
Calculate 'for some reason, the cell is not updating it value when using
RAND() on the spreadsheet

arr(X) = Cells(rowLocation, colLocation).Value

Next X

'compute average value

expval = Application.WorksheetFunction.Average(arr)

End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default recalculating RAND() inside a VBA loop


It worked for me in xl2002. However I had to correct the array dimension
so the average was correct... ReDim arr(1 To loops)

ReDim arr(loops) is the same as ReDim arr(0 To loops)
--
Jim Cone
Portland, Oregon USA





"vbaRay"
wrote in message...
How do I get cells in spreadsheet to recalculate new values using RAND() when
looping inside VBA? I'm trying to compute the average value of a cell who's
dependants contain values generated from the RAND() function.
Sample code is below.
Many thanks for any insights.

Function expval(target As Range, Optional iter As Integer) As Double
'measure value
'record in an array
'recompute value
'repeat
'compute average value

Dim rowLocation As Integer
Dim colLocation As Integer
Dim arr() As Double
Dim loops As Integer
Dim X As Integer

'determine cell location
rowLocation = target.Row
colLocation = target.Column

'set itterations
loops = Application.WorksheetFunction.Max(iter, 20)
'set array
ReDim arr(loops)

'loop through value measurements
For X = 1 To loops
Calculate 'for some reason, the cell is not updating it value when using
RAND() on the spreadsheet
arr(X) = Cells(rowLocation, colLocation).Value
Next X

'compute average value
expval = Application.WorksheetFunction.Average(arr)
End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default recalculating RAND() inside a VBA loop

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

vbaRay wrote:

How do I get cells in spreadsheet to recalculate new values using RAND() when
looping inside VBA? I'm trying to compute the average value of a cell who's
dependants contain values generated from the RAND() function. Sample code is
below.

Many thanks for any insights.

Function expval(target As Range, Optional iter As Integer) As Double

'measure value
'record in an array
'recompute value
'repeat
'compute average value

Dim rowLocation As Integer
Dim colLocation As Integer
Dim arr() As Double
Dim loops As Integer
Dim X As Integer

'determine cell location
rowLocation = target.Row
colLocation = target.Column

'set itterations
loops = Application.WorksheetFunction.Max(iter, 20)

'set array
ReDim arr(loops)

'loop through value measurements
For X = 1 To loops
Calculate 'for some reason, the cell is not updating it value when using
RAND() on the spreadsheet

arr(X) = Cells(rowLocation, colLocation).Value

Next X

'compute average value

expval = Application.WorksheetFunction.Average(arr)

End Function


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default recalculating RAND() inside a VBA loop

And by fails, I mean that the code continues, but the cell's value doesn't
change.

Dave Peterson wrote:

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

vbaRay wrote:

How do I get cells in spreadsheet to recalculate new values using RAND() when
looping inside VBA? I'm trying to compute the average value of a cell who's
dependants contain values generated from the RAND() function. Sample code is
below.

Many thanks for any insights.

Function expval(target As Range, Optional iter As Integer) As Double

'measure value
'record in an array
'recompute value
'repeat
'compute average value

Dim rowLocation As Integer
Dim colLocation As Integer
Dim arr() As Double
Dim loops As Integer
Dim X As Integer

'determine cell location
rowLocation = target.Row
colLocation = target.Column

'set itterations
loops = Application.WorksheetFunction.Max(iter, 20)

'set array
ReDim arr(loops)

'loop through value measurements
For X = 1 To loops
Calculate 'for some reason, the cell is not updating it value when using
RAND() on the spreadsheet

arr(X) = Cells(rowLocation, colLocation).Value

Next X

'compute average value

expval = Application.WorksheetFunction.Average(arr)

End Function


--

Dave Peterson


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
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


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default recalculating RAND() inside a VBA loop

Calling the function from another function or sub wouldn't be a problem.

It'll be a problem when the function originates in a cell on a worksheet.

We'll see (maybe) who guessed right <vbg.

Jim Cone wrote:

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


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
use time functions inside if loop saedeepu Excel Worksheet Functions 2 September 17th 09 12:55 PM
Can an array be redim inside a for loop? kcenac Excel Programming 2 April 19th 08 06:46 PM
form inside a loop matt Excel Programming 2 February 26th 07 05:54 PM
Increment stopvalue inside a For loop Revenger Excel Programming 2 May 26th 06 09:17 AM
Create an array inside a For...Next loop Eric Winegarner[_2_] Excel Programming 3 October 27th 05 12:26 AM


All times are GMT +1. The time now is 09:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"