I have tried a variety of methods, mainly those that copilot have given me, to try and have the caption for these toggelbuttons I have created dynamically in the code to change captions when clicked. I have used .OnAction, .OnClick, directly calling the sub I want it to enter, having the sub be in a separate module that is called, and many more that I cannot remember off of the top of my head. Nothing seems to be entering the other sub that allows the buttons to change their caption when click. Would very much appreciate the help! The current state of the code is below.
Private Sub UserForm_Initialize()
Dim i As Integer
Dim numItems1 As Integer
Dim numItems2 As Integer
Dim tglButton As MSForms.ToggleButton
Dim lbl As MSForms.Label
Dim lblCaption As MSForms.Label
Dim lbl1 As MSForms.Label
Dim lbl2 As MSForms.Label
Dim sheetName As String
Dim foundRange As Range
'Dim frames As UserForm
'Properties("Font").Value.Item("Name") = "Arial"
Label1.Font.Size = 10
'Get the sheet name from cell BE2 of the "Costbook" sheet
sheetName = Worksheets("Costbook").Range("BE2").Value
'Get the number of line items in column B of the sheet for the first column
numItems1 = Worksheets(sheetName).Range("B7").End(xlDown).Row - 6
'Get the number of line items in column B of the sheet for the second column
numItems2 = Worksheets(sheetName).Range("B41").End(xlDown).Row - 40
'Create the first frame
Set lbl1 = Me.Controls.Add("Forms.Label.1", "MainCompLbl", True)
lbl1.Caption = "Main Components"
lbl1.Left = 20
lbl1.Top = 40
lbl1.Height = 20 + numItems1 * 20
lbl1.Width = 80
'Loop through first column
For i = 1 To numItems1
'initialize toggle button
Set tglButton = Me.Controls.Add("Forms.ToggleButton.1", "ToggleButton" & i + numItems1, True)
With tglButton
.Caption = "PPT"
.Left = 100
.Top = 50 + (i - 1) * 25
.Height = 20
.Width = 40
' Assign the event handler directly
'.OnAction = "ToggleButton_Click"
End With
'create labels for toggle buttons
Set lblCaption = Me.Controls.Add("Forms.Label.1", "Label" & i, True)
With lblCaption
.Caption = Worksheets(sheetName).Range("B" & i + 6).Value & ":" ' Set label text
.Left = tglButton.Left - tglButton.Width - 40 ' Adjust position
.Top = tglButton.Top
.Width = 70
.Height = 20
End With
'checks to see if user clicked to include the component in the frame
Set foundRange = Worksheets("SAVED DATA").Range("A:A").Find(tglButton.Caption, LookIn:=xlValues, LookAt:=xlPart)
If Not foundRange Is Nothing Then
If foundRange.Offset(0, 1).Value = "False" Then
tglButton.Enabled = False
End If
End If
Next i
'Create label for second column
Set lbl2 = Me.Controls.Add("Forms.Label.1", "AddLbl1", True)
lbl2.Caption = "Additional Parts/Processes"
lbl2.Left = 190
lbl2.Top = 40
lbl2.Height = 20 + numItems2 * 20
lbl2.Width = 100
'loop through and create toggle buttons and labels for second column
For i = 1 To numItems2
Set tglButton = Me.Controls.Add("Forms.ToggleButton.1", "ToggleButton" & i + numItems1, True)
With tglButton
.Caption = "PPT"
.Left = 270
.Top = 50 + (i - 1) * 25
.Height = 20
.Width = 40
' Assign the event handler directly
'.OnAction = "ToggleButton_Click"
End With
'create labels for togglebox
Set lblCaption = Me.Controls.Add("Forms.Label.1", "Label" & i, True)
With lblCaption
.Caption = Worksheets(sheetName).Range("B" & i + 40).Value & ":" ' Set label text
.Left = tglButton.Left - tglButton.Width - 40 ' Adjust position
.Top = tglButton.Top
.Width = 70
.Height = 20
End With
'checks to see if user clicked to include the component in the frame
Set foundRange = Worksheets("SAVED DATA").Range("A:A").Find(tglButton.Caption, LookIn:=xlValues, LookAt:=xlPart)
If Not foundRange Is Nothing Then
If foundRange.Offset(0, 1).Value = "False" Then
tglButton.Enabled = False
End If
End If
'checks to see if it is one of the few that is not the exact same name
If UCase(tglButton.Caption = "FRAME AND CYLINDER ASSEMBLY - SHOP 22") Then
If Worksheets("SAVED DATA").Range("B14").Value = "False" Then
tglButton.Enabled = False
End If
End If
If UCase(tglButton.Caption = "COMPRESSOR RUN TEST") Then
If Worksheets("SAVED DATA").Range("B15").Value = "False" Then
tglButton.Enabled = False
End If
End If
If UCase(tglButton.Caption = "STANDARD BLUE FINISH PAINT") Then
If Worksheets("SAVED DATA").Range("B16").Value = "False" Then
tglButton.Enabled = False
End If
End If
Next i
End Sub
' Event handler for toggle button click
Public Sub ToggleButton_Click()
Dim clickedButton As MSForms.ToggleButton
Set clickedButton = Me.ActiveControl
' Change caption from "PPT" to "Naroda"
If clickedButton.Caption = "PPT" Then
clickedButton.Caption = "Naroda"
Else
clickedButton.Caption = "PPT"
End If
End Sub
Private Sub ToggleButton_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If ToggleButton.Value = True Then
ToggleButton.Caption = "Naroda"
ToggleButton.Tag = "Naroda"
Else
ToggleButton.Caption = "PPT"
ToggleButton.Tag = "PPT"
End If
End Sub
I have pressed F9 to have an automatic stop in the other sub that has the if statement to detmine what the caption should be when pressed and I have yet to hit that stop, meaning that I have never been able to get it to enter the sub.