OK, I came up with something that should work.
You may have to modify slightly to fit your structure.
Let's say that you are wanting to populate the range A1:T1 (20 cells) with unrepeating numbers from 1-80, one at a time.
And let's say that you have a bunch of rows of data of 40 numbers, starting at cell A10 and going down.
The first thing we want to do is to set up Conditional Formatting on our current rows of data, to highlight the numbers as they get selected.
So, starting with cell A10, select ALL the rows and columns of existing numbers that you want to check.
Then, go to Conditional Formatting, select New Rule, then "Use a formula to determine which cells to format", and enter this formula:
Excel Formula:
=COUNTIF($A$1:$T$1,A10)>0
then click the Format button, go to the Fill tab, select the color you want to highlight the cell in, and click OK.
Then, add this VBA code to the workbook:
VBA Code:
Sub PopulateRandomNum()
Dim rng As Range
Dim lcol As Long
Dim rn As Long
Dim cell As Range
Dim ct As Long
' Designate range to populate values in
Set rng = Range("A1:T1")
' Check to see if all columns filled in
If Range("T1") <> "" Then
MsgBox "All cells have been populated with random numbers", vbOKOnly, "STOP!"
Exit Sub
End If
' Find next cell to populate
If Range("A1") = "" Then
Set cell = Range("A1")
Else
Set cell = Range("U1").End(xlToLeft).Offset(0, 1)
End If
' Populate next cell
Do
' Calculate random number
rn = CreateRandomNum(1, 80)
' Check to see if it is not found in range
If Application.WorksheetFunction.CountIf(rng, rn) = 0 Then
' Populate cell with random number
cell.Value = rn
' Exit loop
Exit Do
End If
' Fail-safe to make sure you do not get caught in endless loop
ct = ct + 1
If ct = 200 Then
MsgBox "Process failed and quit after 200 attempts.", vbOKOnly, "ERROR!"
Exit Do
End If
Loop
End Sub
Function CreateRandomNum(lb As Long, ub As Long) As Long
Randomize
CreateRandomNum = Int((ub - lb + 1) * Rnd + lb)
End Function
If you like, you can add a button to your worksheet, and attach the "PopulateRandomNum" VBA procedure to the button, or assign a keyboard shortcut to this code to quickly and easily run it (by clicking the button or using the keyboard shortcut).
Each time you run it, it will populate the range A1:T1, one cell at a time.
And the Conditional Formatting will be invoked automatically and highlight the numbers, as they appear in your list.