samedi 19 septembre 2015

Generate truly random numbers in range of cells using vba

I am trying to randomly allocate the cells in range B4:Z24 of size 20 X 25 i.e. total 500 cells should contain total 500 unique values, the range for random value is 1 to 500. Therefore every number can occur only once in the table. Tried with the code below but it generate duplicates in some cells Can anyone help me here ?

Option Explicit
Public Sub Random()

Dim RandomNumber As Integer
Dim i, j As Integer

For j = 2 To 26
Randomize
RandomNumber = Int((500 - 1 + 1) * Rnd + 1)
For i = 4 To 24
With Sheets("Game")
Randomize
RandomNumber = Int((500 - 1 + 1) * Rnd + 1)
Cells(i, j) = RandomNumber
End With
Next i
Next j
End Sub




Aucun commentaire:

Enregistrer un commentaire