Function GenCode(ByVal r As Range) As String
Dim strValues As String
Dim currCode As String
Dim newCar As String
Dim c As Range
For Each c In r.Cells
strValues = strValues & "-" & c.Value
Next c
strValues = Mid(strValues, 2)
anotherCode:
Randomize
Do While Len(currCode) < 4
newCar = Int(Rnd() * 10)
If InStr(currCode, newCar) = 0 Then
currCode = currCode & newCar
End If
Loop
If InStr(strValues, currCode) Then GoTo anotherCode
GenCode = currCode
Set c = Nothing
End Function