✔ 最佳答案
網路搜刮拼湊的....試試
UserForm程序
Option Explicit
Dim UserForm_ToggleButton(1 To 100) As New Class1
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To UBound(UserForm_ToggleButton)
Set UserForm_ToggleButton(i).Class_ToggleButton = Controls("ToggleButton" & i)
Next
End Sub
新增一個物件Class
Option Explicit
Public WithEvents Class_ToggleButton As MSForms.ToggleButton
Private Sub Class_ToggleButton_Click()
If Class_ToggleButton.Value = True Then
Class_ToggleButton.BackColor = &HC0FFC0
Else
Class_ToggleButton.BackColor = &H8000000F
End If
End Sub
還是說要複製100個程序(巨集就很好用)
Sub 複製100()
For i = 1 To 100
Cx = [A65536].End(xlUp).Row + 1
Cells(Cx, 1) = "Private Sub ToggleButton" & i & "_Click()"
Cells(Cx + 1, 1) = "If ToggleButton" & i & ".Value = True Then"
Cells(Cx + 2, 1) = "ToggleButton" & i & ".BackColor = &HC0FFC0"
Cells(Cx + 3, 1) = "Else"
Cells(Cx + 4, 1) = "ToggleButton" & i & ".BackColor = &H8000000F"
Cells(Cx + 5, 1) = "End If"
Cells(Cx + 6, 1) = "Next"
Cells(Cx + 7, 1) = "End Sub"
Next
End Sub