Welcome on board!
Using this code:
- The game starts once you activate the sheet.
- The player can't change the selection made by the code.
First, add this code to worksheet's Module (in worksheet code pane (right-click on the tab and select "View code") put the following):
Private Used_Range As Range, Quiz_Range As Range, ThisCell As Range, PreventSelect As Boolean
Private Sub Worksheet_Activate()
FreshStart
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim n As Long, nMax As Long
OnError GoTo ex
Application.EnableEvents = False
If Quiz_Range Is Nothing Then FreshStart
If Not Used_Range Is Nothing Then
If Used_Range.Address = Quiz_Range.Address Then
If MsgBox("Game Over!" & Chr(10) & "Do you want to start over?", vbYesNo) = vbYes Then
FreshStart
Else
GoTo ex
End If
End If
End If
nMax = Quiz_Range.Cells.Count
n = RandBetween(1, nMax)
If Used_Range Is Nothing Then
Set ThisCell = Quiz_Range.Cells(n)
Set Used_Range = ThisCell
Else
Do Until Intersect(Quiz_Range.Cells(n), Used_Range) Is Nothing
n = n + 1
If n > nMax Then n = 1
Loop
Set ThisCell = Quiz_Range.Cells(n)
Set Used_Range = Union(Used_Range, ThisCell)
End If
Quiz_Range.Cells(n).Select
ex:
Application.EnableEvents = True
PreventSelect = False
End Sub
Function RandBetween(MinInt As Long, MaxInt As Long) As Long
RandBetween = Int((MaxInt - MinInt + 1) * Rnd + MinInt)
End Function
Sub FreshStart()
Set Used_Range = Nothing
Set Quiz_Range = Range("C9:O14")
Quiz_Range.ClearContents
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If PreventSelect Then
ThisCell.Select
MsgBox "You can't select another cell!"
End If
PreventSelect = True
Application.EnableEvents = True
End Sub
Note: This random selector selects the next unused cell if the output of the Rnd function refers to a used cell.
Edit #1
Using the randomizing collection method by @HTH, the code can be much better:
Private coll As Collection, Quiz_Range As Range, ThisCell As Range, PreventSelect As Boolean
Private Sub Worksheet_Activate()
FreshStart
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim n As Long, nMax As Long, m As Long
OnError GoTo ex
Application.EnableEvents = False
If Quiz_Range Is Nothing Then FreshStart
If coll.Count = 0 Then
If MsgBox("Game Over!" & Chr(10) & "Do you want to start over?", vbYesNo) = vbYes Then
FreshStart
Else
GoTo ex
End If
End If
n = Int(1 + Rnd * (coll.Count))
Quiz_Range.Cells(coll(n)).Select
coll.Remove n
ex:
Application.EnableEvents = True
PreventSelect = False
End Sub
Sub FreshStart()
Set Quiz_Range = Range("C9:F14")
SetColl Quiz_Range
Quiz_Range.ClearContents
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If PreventSelect Then
ThisCell.Select
MsgBox "You can't select another cell!"
End If
PreventSelect = True
Application.EnableEvents = True
End Sub
Sub SetColl(rng As Range)
Set coll = New Collection
Dim i As Long
For i = 1 To rng.Count
coll.Add i
Next
End Sub