Generating Random Numbers - Tough One

L

Legacy 5232

Guest
Hi Everyone

I need to generate about 2695 random numbers in column A that are 7 digits long, between 1000000:9999999, do not start with zero, and are not duplicates of each other. Does anyone know the formula?

Thanks
Jacob
 
An attempt to post a sample of the spreadsheet

Jindon,
I tried the last revision on a new blank workbook, and it still isn't generating any random numbers. Here is an attempt to post a shot of the spreadsheet. I'm trying to get random numbers for column A only, and stop generating them at the last row which contains data. Then if a new line of data is added later, a new number will generate, as soon as any data appears on the corresponding row of column D.

This may not be possible. If I am wasting your time, please say so.
Thanks,
for 6-20-06 w jindon random numbers.xls
ABCDEFGHIJKLMN
1Rand#Priority1Priority2TaskDescriptionDept.Givento:%-or-DateAct.Hrs.EntryDateDateWantedOriginatorOtherNotesDone
258008161Makedrippansfor1,2,3,7,8,9,10,13PlasticPrt.Andrew50%606/06/0606/30/06StarnerInprogress
36215667FabricateGuardToCoverAniloxChainDriveOnPress#2PaperPrtFAndrew04/20/0404/27/04Cannefax
43606062Newrails&piecetoCupAPadlockerConveyorFAndrew05/31/0606/15/06RobertSat.orSun.
53717532FabSameBaffles&InstallOnRewind,DriveSide.ToCatchOil&DirectItIntoACatchPan.PaperPrtMAndrew06/03/0506/10/05Edgett
67972660RefitDripPansToAll4Colorheads-Press15PlasticPrtMAndrew03/09/0603/16/06StarnerSat.orSun.
711261581ReplacebuttonsonBaler4SlitBaleBlkBooker04/26/06ASAPMcCoyPartsarehere
878465122Install(2)4'lightsaboveBlanker#4SlitBaleBlkBooker06/02/06ASAPAngelLightsarehere
Current
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Perhaps I should explain, this one is showing random numbers, it is not the one I was testing on. It was from your previous code, which generated numbers far past the last line that had corresponding text in the adjacent row. That code worked, but would by default add numerous rows, which complicate the end use requirement.

Hope that explains...
 
Upvote 0
OK
then you only need following code to the sheet module
When you add new line on col.D and only when correnponding Col.A has no value, the code adds new unique number in Col.A..
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Long
With Target
       If .Count>1 Then Exit Sub
       If .Column <> 4 Then Exit Sub
       If Not IsEmpty(.Offset(,-3)) Then Exit Sub
       If IsEmpty(.Value) Then Exit Sub
End With
Again:
x = Int(Rnd * 900000) + 100000
If Application.CountIf(Range("a:a"),x)>0 Then GoTo Again
Application.EnableEvents = False
Target.Offset(, -3).Value = x
Application.EnableEvents = True
End Sub
 
Upvote 0
Jindon,
Okay,
It is working just as you indicated. It took me a while to figure out a few things, but it is doing exactly what you said it would.

Thank you very much
Sorry for any confussion.
Gary
 
Upvote 0
And this is to fill the random numbers in col.A upto the last row Col.D has value...
Code:
Sub Sample
Dim x As Long, y As Long
y = Range("d" & Rows.Count).End(xlUp).Row
Application.EnableEvents = False
Randomize
With CreateObject("Scripting.Dictionary")
    Do
Again:
        x = Int(Rnd * 9000000) + 100000
        If .exists(x) Then GoTo Again
        .add x, Nothing
    Loop While .Count <=y -1
Range("A2").Resize(.Count) = Application.Transpose(.keys)
End With
Application.EnableEvents = True
End Sub
 
Upvote 0
Jindon,
Seems to run great, thanks.

FYI,
I changed the 90000 to 9000, and the 100000 to 1000, and now I have a four digit number. I think that will be easier for me to keep up with.

Thanks again,
Gary
 
Upvote 0

Forum statistics

Threads
1,221,622
Messages
6,160,887
Members
451,676
Latest member
Assy Bissy

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