Random number, generates only once, 4 digits!

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
I'm trying to generate a series of random numbers. the criteria is that the numbers should only be generated once, and must have 4 digits (e.g. between 0000 and 9999).
I've tried the following in Excel:
Code:
=TRUNC((RAND()*(9999-0)+0),4)
but it still will show a number less than 1000 as 3 digits (or less than 100 as 2 etc), not 4. Also, it calculates with every change to the form.
Would anyone care to help? thanks.
 
Here it is:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B1") <> "" Then
    Range("A1").Value = CInt(((9999 - 1000) * Rnd) + 1000)
Else:
    Range("A1") = ""
End If
End Sub

as you can see if any other value in the worksheet is changed it will recalculate a new number...
 
Upvote 0

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
Try This:

Code:
[color=darkblue]Dim[/color] B_1 [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Worksheet_Change([color=darkblue]ByVal[/color] Target [color=darkblue]As[/color] Range)
    
    [color=darkblue]Call[/color] Test
    B_1 = Range("B1").Value
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[color=darkblue]Sub[/color] Test()
    
    [color=darkblue]If[/color] Range("B1").Value <> B_1 [color=darkblue]Then[/color]
        [color=darkblue]If[/color] Range("B1").Value <> "" [color=darkblue]Then[/color]
            Range("A1").Value = [color=darkblue]CInt[/color](((9999 - 1000) * Rnd) + 1000)
        [color=darkblue]Else[/color]
            Range("A1").Value = " "
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
    
[color=darkblue]End[/color] Sub
 
Upvote 0
What are unique random numbers?
If you mean random numbers in a range..
Yes, we are. The range is 1000~9999
 
Upvote 0

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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