my approach is a little different than yours but it's a non-userform tictactoe nonetheless :)
Play Area is E4:G6, you would need to change it manually in the code
'This is saved on the worksheet itself
Option Explicit
'Public because any "= something" operation is executed on each cell-change
'thus the value has to be defined outside the sub to not reset it each iteration,
'breaking the loop that switches between X and O
Public rCounter As Integer
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rInt As Range
Dim rCell As Range
Dim msg As Variant
Dim xOffset As Integer
Dim yOffset As Integer
'This is the center cell's position
xOffset = 5
yOffset = 6
Set rInt = Intersect(Target, Range("E4:G6"))
If Not rInt Is Nothing Then
For Each rCell In rInt
If rCell.Value = "X" Or rCell.Value = "O" Then
msg = MsgBox("You can't choose this cell!", vbOKOnly + vbInformation)
Else
If rCounter Mod 2 Then
'Change these to interior.color and you have colors instead, you would
'need to adjust the entire code to match that though
rCell.Value = "X"
Else
rCell.Value = "O"
End If
End If
Next
End If
If Cells(xOffset, yOffset).Value = "X" And Cells(xOffset + 1, yOffset).Value = "X" And Cells(xOffset - 1, yOffset).Value = "X" Or Cells(xOffset, yOffset).Value = "X" And Cells(xOffset - 1, yOffset + 1).Value = "X" And Cells(xOffset + 1, yOffset - 1).Value = "X" Or Cells(xOffset, yOffset).Value = "X" And Cells(xOffset - 1, yOffset - 1).Value = "X" And Cells(xOffset + 1, yOffset + 1).Value = "X" Or Cells(xOffset, yOffset - 1).Value = "X" And Cells(xOffset - 1, yOffset - 1).Value = "X" And Cells(xOffset + 1, yOffset - 1).Value = "X" Or Cells(xOffset, yOffset + 1).Value = "X" And Cells(xOffset - 1, yOffset + 1).Value = "X" And Cells(xOffset + 1, yOffset + 1).Value = "X" Then
msg = MsgBox("Player X wins!", vbOKOnly)
wClearTicTacToe
End If
If Cells(xOffset, yOffset).Value = "O" And Cells(xOffset + 1, yOffset).Value = "O" And Cells(xOffset - 1, yOffset).Value = "O" Or Cells(xOffset, yOffset).Value = "O" And Cells(xOffset - 1, yOffset + 1).Value = "O" And Cells(xOffset + 1, yOffset - 1).Value = "O" Or Cells(xOffset, yOffset).Value = "O" And Cells(xOffset - 1, yOffset - 1).Value = "O" And Cells(xOffset + 1, yOffset + 1).Value = "O" Or Cells(xOffset, yOffset - 1).Value = "O" And Cells(xOffset - 1, yOffset - 1).Value = "O" And Cells(xOffset + 1, yOffset - 1).Value = "O" Or Cells(xOffset, yOffset + 1).Value = "O" And Cells(xOffset - 1, yOffset + 1).Value = "O" And Cells(xOffset + 1, yOffset + 1).Value = "O" Then
msg = MsgBox("Player O wins!", vbOKOnly)
wClearTicTacToe
End If
Set rInt = Nothing
Set rCell = Nothing
rCounter = rCounter + 1
End Sub
Sub wClearTicTacToe()
Range("E4:G6").Value = ""
End Sub