UDF results in #VALUE! when invoked from spreadsheet, but OK in single-step (F8)

Status
Not open for further replies.

danleonida

New Member
Joined
Dec 3, 2012
Messages
4
In my application, the user needs control of the seed of a pseudo-random number generator (PRNG). I implemented the RAND() function as a UDF in VBA. It works fine in single-step (F8) but results in #VALUE! when invoked from the spreadsheet. I cannot figure out why! Here's the code:
<o:p></o:p>
Rich (BB code):
Function RND() As Variant
Rich (BB code):
' Implements Excel's RAND() algorithm with full control of the seed.
'
' http://support.microsoft.com/kb/828795
'
' C IX, IY, IZ SHOULD BE SET TO INTEGER VALUES BETWEEN 1 AND 30000 BEFORE FIRST ENTRY
' IX = MOD(171 * IX, 30269)
' IY = MOD(172 * IY, 30307)
' IZ = MOD(170 * IZ, 30323)
' RANDOM = AMOD(FLOAT(IX) / 30269.0 + FLOAT(IY) / 30307.0 + FLOAT(IZ) / 30323.0, 1.0)
'
   Dim Xi      As Long
   Dim Yi      As Long
   Dim Zi      As Long
   Dim LAST_X  As Long
   Dim LAST_Y  As Long
   Dim LAST_Z  As Long

   LAST_X = Range("last_x").Value
   LAST_Y = Range("last_y").Value
   LAST_Z = Range("last_z").Value

   If LAST_X = 0 Or LAST_Y = 0 Or LAST_Z = 0 Then
       LAST_X = Range("seed_x").Value
       LAST_Y = Range("seed_y").Value
       LAST_Z = Range("seed_z").Value
   End If

   Xi = (171 * LAST_X) Mod 30269
   Yi = (172 * LAST_Y) Mod 30307
   Zi = (170 * LAST_Z) Mod 30323

   Range("last_x").Value = Xi ' F8 goes back to top when function dimmed as Variant
   Range("last_y").Value = Yi
   Range("last_z").Value = Zi

   RND = (Xi / 30269 + Yi / 30307 + Zi / 30323) - Int(Xi / 30269 + Yi / 30307 + Zi / 30323)

End Function
<o:p></o:p>
Everything seems to be working fine when I single-step it (F8) but when invoked from the main spreadsheet, it returns the '#VALUE!' error. Hm!
<o:p></o:p>
Here's a 45K file that illustrates the above behaviour:
<o:p></o:p>
2012.11.30...RND.xls
<o:p></o:p>
Hope somebody knows what's 'possessing' the file! Thx!
<o:p></o:p>
danleonida-at-yahoo-dot-com
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Status
Not open for further replies.

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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