Random Number Generator

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
771
Office Version
  1. 365
Platform
  1. Windows
Good Morning,

Is there anyway i can create a random number to load into a userform when it is created.

Many Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Sure - plonk something like this in the Initialize event of the userform:

Code:
Private Sub UserForm_Initialize()

Randomize  

Me.TextBox1.Value = CInt(Rnd * 1000000)  'plonk a 6 digit random number in TextBox1 - amend as appropriate

End Sub
 
Upvote 0
Many Thanks.

Will the random number every be re generated as i will be using it to remove a line of data from a spreadsheet when the enquiry has been dealt with.
 
Upvote 0
There is nothing in the code that enforces no duplicates (so it could be possible to get a subsequent duplicate). If you need to enforce no duplicates, then you need to maintain a record somewhere of all previously generated numbers.

What is the reason for the random number (is it an ID for example)? If it is an ID, I would recommend using a sequential number instead.
 
Upvote 0
Sorry about this.

I have added the VB in but i keep getting an overflow error
"Run-time error "6" - Overflow.

Do you know what this means?

Many Thanks
 
Upvote 0
The reason for the random number was a reference on one of the sheets so i can delete the line once the enquiry has been completed, as it is then entered onto another sheet under a managers name for stat purposes.
 
Upvote 0
Sorry that line should have been:

Rich (BB code):
Randomize  

Me.TextBox1.Value = CLng(Rnd * 1000000)  'plonk a 6 digit random number in TextBox1 - amend as appropriate
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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