Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-23-2012, 06:31 AM
tinfanide tinfanide is offline How to reserve an array in a public function? Windows 7 64bit How to reserve an array in a public function? Office 2010 32bit
Expert
How to reserve an array in a public function?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default How to reserve an array in a public function?

Code:
Sub test()

Dim x As Integer

For x = 0 To 3
    MsgBox (ReturnRandomNumber(0, 3, x))
Next x

End Sub

Public Function ReturnRandomNumber(lower As Integer, upper As Integer, turn As Integer) As String
    
    Dim n As Integer, arr() As Variant
    
Redo:
    n = Int((upper - lower + 1) * Rnd + lower)
    
    ReDim Preserve arr(turn)
    
    ''' I want to store every n to check if the random number generated is repeated
    For Each a In arr
        If a = n Then GoTo Redo
    Next a
    
    ''' Within this function, the Array arr() does not store every n
    arr(turn) = n
    ReturnRandomNumber = n
    
End Function
If I move the foreach loop and the array within the sub, it can loop through the array and check if the newly generated random number is repeated.



But it does not work within a public function.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reserve an array in a public function? Complex array formula andrei Excel 9 02-03-2012 03:40 AM
How to capture start and ending Ref. Nos. in an array KIM SOLIS Excel 5 09-07-2011 07:43 AM
Look up an array based on user input johnsmb Excel 2 01-07-2011 01:12 PM
outlook programmatically read an attachment into byte array chriskaza81 Outlook 0 11-19-2010 01:03 AM
How to reserve an array in a public function? Array into ComboBox + Macro-Text into ActiveDocument Vivi Word VBA 1 01-27-2010 07:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft