標題: 是否要寫30個 Private Sub CheckBox*_Change()才可? [打印本頁] 作者: yangjie 時間: 2011-1-29 00:29 標題: 是否要寫30個 Private Sub CheckBox*_Change()才可?
請教於各位板主大大:
Private Sub CheckBox1_Change()
ListBox1.Clear
For i = 1 To 27
If Controls("CheckBox" & i).Value = True Then
x = Controls("CheckBox" & i).Caption
ListBox1.AddItem x
End If
Next
End Sub
以上情形若 CheckBox1 之外尚有CheckBox2 CheckBox3 CheckBox4........Change() 共30個
是否要寫30個 Private Sub CheckBox*_Change()
可否另有 Loop 方式??? 或其他簡化方式
謝謝指導作者: Hsieh 時間: 2011-1-29 00:45
請教Hsieh版主:
自己嘗試看看
Option Explicit
Dim newcontrol() As New Class1
Private Sub UserForm_Initialize()
ReDim newcontrol(1 To 27)
For I = 1 To 27
Set newcontrol(I).Comd = Controls("CheckBox" & I)
Next
End Sub
於類別模組下
Public WithEvents Comd As MSForms.CommandButton
Private Sub Comd_Click()
ListBox1.Clear
For I = 1 To 27
If Controls("CheckBox" & I).Value = True Then
x = Controls("CheckBox" & I).Caption
ListBox1.AddItem x
End If
Next
End Sub
不知哪裡出差錯???作者: yangjie 時間: 2011-1-29 01:34
謝謝Hsieh版主: OK 了 做以下修正就OK了
Option Explicit
Dim newcontrol() As New Class1
Private Sub UserForm_Initialize()
ReDim newcontrol(1 To 27)
For I = 1 To 27
Set newcontrol(I).Comd = Controls("CheckBox" & I)
Next
End Sub
於類別模組下
Public WithEvents Comd As MSForms.CheckBox
Private Sub Comd_Click()
userform3.ListBox1.Clear
For I = 1 To 27
If userform3.Controls("CheckBox" & I).Value = True Then
x = userform3.Controls("CheckBox" & I).Caption
userform3.ListBox1.AddItem x
End If
Next
End Sub作者: yangjie 時間: 2011-1-29 01:39