PDA

View Full Version : Solved: Userform textbox - random numbers (Excel '97)



phendrena
03-31-2009, 09:34 AM
Hi,

Would anyone be able to suggest how to generate a 6 digit random number in vba so that I can then place it into a textbox on a userform?

Thanks

Kenneth Hobs
03-31-2009, 11:07 AM
If you mean between 999,999 and 100,000 then something like this in a Module. You can use the Test to check it.
Sub Test()
Debug.Print RBetween(100000, 999999)
End Sub

Function RBetween(lowerbound As Long, upperbound As Long) As Long
RBetween = RBetween = WorksheetFunction.Floor((upperbound - lowerbound + 1) * Rnd + lowerbound, 1)
End Function

To update the textbox during the Initilize event or a commandbutton click event:
Private Sub UserForm_Initialize()
TextBox1.Value = RBetween(999999, 100000)
End Sub


Private Sub CommandButton1_Click()
TextBox1.Value = RBetween(999999, 100000)
End Sub

phendrena
04-01-2009, 03:22 AM
Thank Ken thats worked nicely :)