VBA FORM 內有100個 togglebutton?

2016-10-30 2:44 pm
請問,我有製作一個 FORM 內有 100個 togglebutton,這100個執行動作為,ON是一個顏色,OFF是一個顏色。請問有語法可批次定義此100個按鈕嗎?不需一個一個寫入程式

謝謝

回答 (1)

2016-10-31 10:01 am
✔ 最佳答案
網路搜刮拼湊的....試試

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


收錄日期: 2021-05-03 13:54:33
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20161030064410AAj6eph

檢視 Wayback Machine 備份