Random 6-digit or 9-digit numbers that do not repeat and do not start with 0 (zero)

PETRODATAjames

New Member
Joined
Aug 20, 2022
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I need a formula that will create random and no duplicate numbers in column A. It would be nice to choose if I wanted 6, 7, 8, or 9-digit numbers in the formula. I would also like to export the numbers to a text file, the format would be like word wrap in notepad. All these numbers would be separated by a comma and no spaces.

If the random number width is 6-digits, then the exported records would be - e.g. 123456,234561,345612,456123,561234,612345

LMK if this is something possible.
Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hello! I hope this helps:
VBA Code:
Option Explicit
Sub test()
  Dim myNum As Long, i As Long, random As Long, digits As Integer, myArray As Variant
  With Application
  myNum = .InputBox("How many random numbers do you want?")
  digits = .InputBox("How many digits?")
  ReDim myArray(1 To myNum, 1 To 1)
  For i = 1 To myNum
    Do
      random = (Abs(Timer * Now() * Rnd) Mod (((10 ^ digits) - 1) - (10 ^ (digits - 1)) + 1) + (10 ^ (digits - 1)))
    Loop While Not IsError(.Match(random, myNum, 0))
    myArray(i, 1) = random
  Next
  Range("A1").Resize(myNum).Value2 = myArray
  End With

  Dim myString As String
  For Each arr In myArray
    myString = myString & arr & ","
  Next
  Dim TextFile As Integer
  Dim myFile As String
 
  myFile = "C:\Users\Name\Documents\textfile.txt"
  TextFile = FreeFile
  Open myFile For Output As TextFile
  Print #TextFile, Left(myString, Len(myString) - 1)
  Close #TextFile
End Sub
 
Last edited by a moderator:
Upvote 0
Small fix:
VBA Code:
Option Explicit
Sub test()
  Dim myNum As Long, i As Long, random As Long, digits As Integer, myArray As Variant
  With Application
  myNum = .InputBox("How many random numbers do you want?")
  digits = .InputBox("How many digits?")
  ReDim myArray(1 To myNum, 1 To 1)
  For i = 1 To myNum
    Do
      random = .RandBetween(10 ^ (digits - 1), (10 ^ digits) - 1)
    Loop While Not IsError(.Match(random, myNum, 0))
    myArray(i, 1) = random
  Next
  Range("A1").Resize(myNum).Value2 = myArray
  End With

  Dim myString As String, arr As Variant
  Dim TextFile As Integer
  Dim myFile As String
  
  For Each arr In myArray
    myString = myString & arr & ","
  Next
 
  myFile = "C:\Users\Name\Documents\textfile.txt"
  TextFile = FreeFile
  Open myFile For Output As TextFile
  Print #TextFile, Left(myString, Len(myString) - 1)
  Close #TextFile
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,377
Members
449,097
Latest member
Jabe

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