Private nextNumber As Long
Private lastNumber As Long
Private startTime As Date
Private totalTime As Date
Private previousCell As Range
Private previousCellColor As Variant
Public Sub SetUpSchulteTable(rowCount As Long, columnCount As Long, firstCell As Range)
Dim i As Long
Dim j As Long
Dim k As Long
For i = 0 To rowCount - 1
For j = 0 To columnCount - 1
firstCell.Offset(i, j).Value = i * rowCount + j + 1
Next j
Next i
nextNumber = 1
lastNumber = rowCount * columnCount
Set previousCell = firstCell
previousCellColor = GetCellColor(previousCell)
Randomize
For i = 0 To lastNumber - 1
j = Int(Rnd * lastNumber)
k = firstCell.Offset(i Mod columnCount, Int(i / rowCount)).Value
firstCell.Offset(i Mod columnCount, Int(i / rowCount)).Value = firstCell.Offset(j Mod columnCount, Int(j / rowCount)).Value
firstCell.Offset(j Mod columnCount, Int(j / rowCount)).Value = k
Next i
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Rows.Count > 1 Or Target.Columns.Count > 1 Then Exit Sub
If previousCell Is Nothing Then Exit Sub
If nextNumber = 0 Then Exit Sub
If Target.Value = nextNumber Then
If nextNumber = 1 Then startTime = Now
SetCellColor previousCell, previousCellColor
previousCellColor = GetCellColor(Target)
SetCellColor Target, RGB(192, 255, 192)
If nextNumber = lastNumber Then
totalTime = Now - startTime
MsgBox totalTime * 24 * 60 * 60 & " seconds", vbInformation + vbOKOnly, "Schulte Table"
SetCellColor Target, previousCellColor
nextNumber = 0
Else
Set previousCell = Target
nextNumber = nextNumber + 1
End If
End If
End Sub
Private Function GetCellColor(thisCell As Range) As Variant
If thisCell.Interior.ColorIndex = xlColorIndexNone Then
GetCellColor = xlColorIndexNone
Else
GetCellColor = thisCell.Interior.Color
End If
End Function
Private Sub SetCellColor(thisCell As Range, thisColor As Variant)
If thisColor = xlColorIndexNone Then
thisCell.Interior.ColorIndex = xlColorIndexNone
Else
thisCell.Interior.Color = thisColor
End If
End Sub
Public Sub SetUp5x5Table()
Me.SetUpSchulteTable 5, 5, Range("$A$1")
End Sub