jeudi 9 juin 2016

Generate one random 5 digit number not present in a column of numbers

I have a list of 5 digit numbers. I need to randomly generate a 5 digit number that is not part of this list. I would like to achieve this on click of a button or simply by using a formula.
For example : If this is my column A in excel with 5 digit numbers,

11111  
11113  
11115  

On click of a button I want to generate a unique 5 digit number which is not in this column. I am using this function but it is only matching with the first cell. When I click on my button it gives me all numbers from 11110 to 11116 except 11111. I need it to check with full column and return me a unique value.

Private Sub CommandButton1_Click()

Dim rw As Long
    For rw = 1 To 1
        Cells(rw, 2) = Int((11116 - 11110 + 1) * Rnd + 11110)
        Do Until Cells(rw, 2).Value <> Cells(rw, 1).Value
            Cells(rw, 2) = Int((11116 - 11110 + 1) * Rnd + 11110)
        Loop
    Next rw
End Sub




Aucun commentaire:

Enregistrer un commentaire