• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Random code creation

Uri

New Member
I have a macro that creates in two files with names that complies with a convention. The convention includes also a random number of 4 digits. To create this code I am using the following function to create a number in the range [1111..9999] :

>>> use code - tags <<<
Code:
Function random_number() As String
Dim myRnd As Integer
myRnd = Int(1000 + Rnd * (9999 - 1111 + 1))
random_number = Trim(Str(myRnd))
End Function
In all the tests that I ran the number for each of the two files was different but in each run each file got always the same number.
When I ran a test with only this function and no other code I got each time a different number.
Any idea how this happens?
 
Last edited by a moderator:
You just forgot the Randomize statement in your main macro procedure as explained in VBA help, a must read !​
Another way demonstration just with an Excel basics, a worksheet function :​
Code:
Function iRandom%()
         iRandom = Application.RandBetween(1111, 9999)
End Function

Sub Demo1()
    MsgBox iRandom, 64, "iRandom"
End Sub
Do you like it ? So thanks to click on bottom right Like !​
 
  • Like
Reactions: Uri
Back
Top