Hello rdroy1954,
This is a late answer to your post but I hope you will find it useful. The attached workbook has your User form with a modification. Each vertical column has two player buttons "X" an "O". There are 12 total. When the user clicks a button that player's mark is added to the next cell in that column. After each player makes a choice, the macro checks for a win.
I basically rewrote the code. While your code was working, it lacked the flexibility to accommodate the new changes. You will see this code uses a variety of techniques to simply the and streamline the code. If you were to turn this in as your own project, it would be obvious you had help. My goal is help you see and learn new ways of solving problems with VBA. The code is heavily documented but I am sure you will still have some questions. Please don't hesitate to ask me anything you have a question about.
Here is the User Form code. The attached workbook contains the new changes and this code.
Option Explicit
Dim Board As New Collection
Dim Checker As Boolean
Dim Players() As Variant
Private Sub EnablePlayerButtons(ByVal State As Boolean)
Dim n As Long
' Enable or disable the player "X" and "O" buttons.
For n = 1 To 12
With Me.Controls("CommandButton" & n)
.Enabled = State
End With
Next n
End Sub
Private Sub ClearBoard()
Dim n As Long
' Remove all the players' marks from the board.
For n = 1 To 36
With Me.Controls("TTTbtn" & n)
.Caption = ""
.BackColor = vbButtonFace
End With
Next n
End Sub
Private Sub ClearPlayerCounts()
ReDim Players(1 To 2, 1 To 2)
' Subscripts 1,1 and 2,1 hold the matching cell counts for players.
' A player must have 4 consecutive cells to win.
' The players marks.
Players(1, 2) = "X"
Players(2, 2) = "O"
End Sub
Private Sub AddPlayersMark(ByRef Btn As Object)
Dim col As Integer
Dim n As Integer
Dim Player As Integer
Dim row As Integer
' Get the column associated with the player's button.
col = CInt(Btn.Tag)
' Get the number of the player button that was clicked.
' They are named CommandButton1 - CommandButton12.
n = CInt(Right(Btn.Name, Len(Btn.Name) - 13))
' Find the next empty space on the board for the column selected
' and add the player's mark to the board cell.
For row = 1 To 6
With Board(row & "," & col)
If .Caption = "" Then
Select Case n Mod 2
Case 1: Player = 1: .Caption = Players(1, 2): Exit For
Case 0: Player = 2: .Caption = Players(2, 2): Exit For
End Select
End If
End With
Next row
' Enable only one player's buttons at a time.
If Player = 1 Then
For n = 1 To 11 Step 2
Me.Controls("CommandButton" & n).Enabled = False
Me.Controls("CommandButton" & n + 1).Enabled = True
Next n
End If
' Enable only one player's buttons at a time.
If Player = 2 Then
For n = 1 To 11 Step 2
Me.Controls("CommandButton" & n).Enabled = True
Me.Controls("CommandButton" & n + 1).Enabled = False
Next n
End If
'Check for a winner.
If Player <> 0 Then
Call CheckForWinner(row & "," & col)
End If
End Sub
Private Sub CheckForWinner(ByVal Btn_Name As String)
Dim col As Long
Dim j As Long
Dim k As Long
Dim n As Long
Dim Player As Integer
Dim row As Long
' Check all six directions from the current board cell chosen.
' Reset the Players' marks count to zero.
ClearPlayerCounts
' Determine the row and column on the board for the button that was clicked.
row = Split(Btn_Name, ",")(0)
col = Split(Btn_Name, ",")(1)
' Get the player number by matching the caption of the player button to the players marks.
For n = 1 To UBound(Players)
If Board(row & "," & col).Caption = Players(n, 2) Then
Player = n
End If
Next n
' Check / Diagonal entries.
' Moving Up the vector.
ClearPlayerCounts
j = row
k = col
Do
GoSub TestForWin
k = k + 1
j = j + 1
If j > 6 Or k > 6 Then Exit Do
Loop
' Check / Diagonal entries.
' Moving Down the vector.
ClearPlayerCounts
j = row
k = col
Do
GoSub TestForWin
k = k - 1
j = j - 1
If j < 1 Or k < 1 Then Exit Do
Loop
' Check \ Diagonal entries.
' Moving Up the vector.
ClearPlayerCounts
j = row
k = col
Do
GoSub TestForWin
k = k + 1
j = j - 1
If j < 1 Or k > 6 Then Exit Do
Loop
' Check \ Diagonal entries.
' Moving Down the vector.
ClearPlayerCounts
j = row
k = col
Do
GoSub TestForWin
k = k - 1
j = j + 1
If j > 6 Or k < 1 Then Exit Do
Loop
' Check - Horizontal entries.
ClearPlayerCounts
j = row
k = col
For k = 1 To 6
GoSub TestForWin
Next k
' Check | Vertical entries.
ClearPlayerCounts
j = row
k = col
For j = 1 To 6
GoSub TestForWin
Next j
Exit Sub
' Player must have 4 consecutive board cells with his or her mark in them to win.
TestForWin:
DoEvents
If Board(j & "," & k).Caption = Players(Player, 2) Then
Players(Player, 1) = Players(Player, 1) + 1
' Check for a win.
If Players(Player, 1) = 4 Then
MsgBox "Player """ & Players(Player, 2) & """ Wins!"
EnablePlayerButtons State:=False
' Update the winner's game total.
Select Case Player
Case 1: Me.Label3.Caption = CInt(Me.Label3.Caption) + 1
Case 2: Me.Label4.Caption = CInt(Me.Label4.Caption) + 1
End Select
Exit Sub
End If
Else
' Player was not a winner. Clear the board cell count.
Players(Player, 1) = 0
End If
Return
End Sub
Private Sub CommandButton1_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton10_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton11_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton12_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton2_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton3_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton4_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton5_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton6_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton7_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton8_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub CommandButton9_Click()
AddPlayersMark Me.Frame1.ActiveControl
End Sub
Private Sub TTTbtn37_Click()
' Reset Game
ClearBoard
EnablePlayerButtons State:=True
End Sub
Private Sub TTTbtn38_Click()
' New Game
Call ClearBoard
Me.Label3.Caption = "0"
Me.Label4.Caption = "0"
EnablePlayerButtons State:=True
End Sub
Private Sub UserForm_Initialize()
Dim Btn As Object
Dim j As Integer
Dim k As Integer
Dim n As Integer
ClearPlayerCounts
' Set the Tag equal to the column number for the player buttons "X" and "O".
For k = 1 To 12 Step 2
n = n + 1
Me.Frame1.Controls("CommandButton" & k).Tag = n
Me.Frame1.Controls("CommandButton" & (k + 1)).Tag = n
Next k
n = 0
' Save the Board cells (CommandButtons) and their x,y coordinates in the collection named Board.
' The collection returns a Board cell (CommandButton object) referenced by "x,y".
For j = 31 To 1 Step -6
n = n + 1
' Save the column the cell belongs to using the Tag property.
For k = 0 To 5
Me.Frame1.Controls("TTTbtn" & (j + k)).Tag = n
Set Btn = Me.Frame1.Controls("TTTbtn" & (j + k))
Board.Add Btn, n & "," & (k + 1)
Next k
Next j
End Sub
Bookmarks