Want to play Tic Tac Toe in Microsoft Excel? Learn how to create a fun and interactive Tic Tac Toe game using Excel and VBA! This step-by-step tutorial will guide you through the process, making it easy for beginners to follow along. Impress your friends and improve your Excel skills with this creative project. Watch now and start coding your own game!
Code:
Note:- Replace * with not equal sign (5 places) highlighted with bold
Private Sub Worksheet_Change(ByVal Target As Range)
' Check if the changed cell is within the Tic-Tac-Toe grid (B2:D4)
If Not Intersect(Target, Range("B2:D4")) Is Nothing Then
CheckWinner
End If
End Sub
Sub CheckWinner()
Dim board(1 To 3, 1 To 3) As String
Dim i As Integer, j As Integer
Dim winner As String
' Load the board
For i = 1 To 3
For j = 1 To 3
board(i, j) = UCase(Cells(i + 1, j + 1).Value)
Next j
Next i
' Check rows and columns
For i = 1 To 3
If board(i, 1) = board(i, 2) And board(i, 2) = board(i, 3) And board(i, 1) * "" Then
winner = board(i, 1)
ElseIf board(1, i) = board(2, i) And board(2, i) = board(3, i) And board(1, i) * "" Then
winner = board(1, i)
End If
Next i
' Check diagonals
If board(1, 1) = board(2, 2) And board(2, 2) = board(3, 3) And board(1, 1) * "" Then
winner = board(1, 1)
ElseIf board(1, 3) = board(2, 2) And board(2, 2) = board(3, 1) And board(1, 3) * "" Then
winner = board(1, 3)
End If
' Display the winner
If winner * "" Then
MsgBox winner & " wins!", vbInformation
' Optionally, clear the board for a new game
Range("B2:D4").ClearContents
End If
End Sub
Note:- Replace * with not equal sign (5 places) highlighted with bold
Информация по комментариям в разработке