Sub RandomNumberGenerator()
'Generates all the numbers between 0 - 20 and saves them to a history log.
Dim RndNmbr As Integer
RndNmbr = Int((20 - 0 + 1) * Rnd + 0) 'The syntax = Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
'Sheets("Main").Range("A5").Value = RndNmbr
MsgBox RndNmbr
'Writes the value to the history log:
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = RndNmbr
End Sub
Sub RandomNumberGenerator2()
'Generates all the numbers between 0 - 20 and saves them to a history log.
'Once all the numbers have been generated, clears the history and starts over again.
Dim RndNmbr As Integer
Dim History As Range
'Sets the range to look if the RndNmbr has already been generated before:
With Sheets(2)
Set History = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
'When all the numbers have been generated:
If History.Rows.Count = 20 Then
History.Clear 'Clears the history
Set History = .Range("A2") 'Starts over again
End If
End With
Generate:
RndNmbr = Int((20 - 0 + 1) * Rnd + 0) 'The syntax = Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)
'Generare another RndNmbr if the current rndnbr was found from the history
If Application.WorksheetFunction.CountIf(History, RndNmbr) > 0 Then GoTo Generate
'Sheets("Main").Range("A5").Value = RndNmbr
MsgBox RndNmbr
'Writes the value to the history log:
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = RndNmbr
End Sub