I'm looking for assistance with modifying the following VBA code; this code auto-generates 4-digit unique numbers, using zero as one of the starting digit as well. I need the 4-digit numbers NOT to begin with a zero, the 4-digit numbers should only begin with numbers 1-9.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim Test As Integer
If Target.Column <> 1 Then Exit Sub
Application.EnableEvents = False
For Each Cell In Target
With Cell.Offset(0, 1)
If IsEmpty(Cell) Then
.ClearContents
Else
.NumberFormat = "0000"
Do
.Value = Int(9999 * Rnd())
Test = WorksheetFunction.CountIf(.EntireColumn, .Value)
Loop Until Test <= 1
End If
End With
Next Cell
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim Test As Integer
If Target.Column <> 1 Then Exit Sub
Application.EnableEvents = False
For Each Cell In Target
With Cell.Offset(0, 1)
If IsEmpty(Cell) Then
.ClearContents
Else
.NumberFormat = "0000"
Do
.Value = Int(9999 * Rnd())
Test = WorksheetFunction.CountIf(.EntireColumn, .Value)
Loop Until Test <= 1
End If
End With
Next Cell
Application.EnableEvents = True
End Sub
Last edited: