Hi all.
I have the general-purpose class module (clsBpca) which I designed to be able to settle
an event handling of plural control as Control-Array.
Please look at the following figure and the site about the structure.
(Because I do not protect the macro, you can watch the macro of the class modules.)
[ Illustration of clsBpca Concept ]
http://www.h3.dion.ne.jp/~sakatsu/GifImg/GifDoc/Bpca_Concept.gif
[ clsBpca document ]
AddinBox( Breakthrough in the Pseudo Control Array )
[ Usage example ]
AddinBox( Breakthrough in the Pseudo Control Array )
[ Download & Reference guide ]
AddinBox( Breakthrough in the Pseudo Control Array : clsBpca Reference )
The preparations are completed just to import the class module mentioned above in your workbook.
It is not necessary for you to modify the code of the class module.
a) You can program it as follows when you use clsBpca.
b) The grouping of TextBox/CheckBox/OptionButton in the following example is suitable.
You may summarize it in one or may divide it into some more.
You should only do similar coding.
c) In the following example, I validate Change/Exit event.
Other events are available if you appoint the necessary events in the argument of the Rgst method.
d) In clsBpca, the spelling of the Exit event procedure becomes "OnExit".
Code:
-- UserForm module --
Private WithEvents TextGrp1 As clsBpca '1-50
Private WithEvents TextGrp2 As clsBpca '51-100
Private WithEvents ChkGrp1 As clsBpca '1-30
Private WithEvents ChkGrp2 As clsBpca '31-60
Private WithEvents OptGrp1 As clsBpca '1-20
Private Sub UserForm_Initialize()
Dim j As Integer
Set TextGrp1 = New clsBpca
With TextGrp1
For j = 1 To 50
.Add Me.Controls("TextBox" & j)
Next j
.Rgst BPCA_Change + BPCA_Exit
End With
Set TextGrp2 = New clsBpca
With TextGrp2
For j = 51 To 100
.Add Me.Controls("TextBox" & j)
Next j
.Rgst BPCA_Change + BPCA_Exit
End With
Set ChkGrp1 = New clsBpca
With ChkGrp1
For j = 1 To 30
.Add Me.Controls("CheckBox" & j)
Next j
.Rgst BPCA_Change
End With
Set ChkGrp2 = New clsBpca
With ChkGrp2
For j = 31 To 60
.Add Me.Controls("CheckBox" & j)
Next j
.Rgst BPCA_Change
End With
Set OptGrp1 = New clsBpca
With OptGrp1
For j = 1 To 20
.Add Me.Controls("OptionButton" & j)
Next j
.Rgst BPCA_Change
End With
End Sub
Private Sub UserForm_Terminate()
TextGrp1.Clear
TextGrp2.Clear
ChkGrp1.Clear
ChkGrp2.Clear
OptGrp1.Clear
Set TextGrp1 = Nothing
Set TextGrp2 = Nothing
Set ChkGrp1 = Nothing
Set ChkGrp2 = Nothing
Set OptGrp1 = Nothing
End Sub
' All events of controls are handled by these one procedure in UserForm module.
'-----------------------------------------------------
Private Sub ChkGrp1_Change(ByVal Index As Integer) 'CheckBox1-30
If (ChkGrp1.Item(Index).Value = True) Then
'ON processing
MsgBox ChkGrp1.Item(Index).Name & " [ON]"
Else
'OFF processing
MsgBox ChkGrp1.Item(Index).Name & " [OFF]"
End If
End Sub
'-----------------------------------------------------
Private Sub ChkGrp2_Change(ByVal Index As Integer) 'CheckBox31-60
If (ChkGrp2.Item(Index).Value = True) Then
'ON processing
MsgBox ChkGrp2.Item(Index).Name & " [ON]"
Else
'OFF processing
MsgBox ChkGrp2.Item(Index).Name & " [OFF]"
End If
End Sub
'-----------------------------------------------------
Private Sub OptGrp1_Change(ByVal Index As Integer) 'OptionButton1-20
If (OptGrp1.Item(Index).Value = True) Then
'ON processing
MsgBox OptGrp1.Item(Index).Name & " [ON]"
Else
'OFF processing
MsgBox OptGrp1.Item(Index).Name & " [OFF]"
End If
End Sub
'-----------------------------------------------------
Private Sub TextGrp1_Change(ByVal Index As Integer) 'TextBox1-50
'Any processing for TextGrp1.Item(Index)
End Sub
Private Sub TextGrp1_OnExit(ByVal Index As Integer, _
ByVal Cancel As MSForms.ReturnBoolean) 'TextBox1-50
If ( Any invalid condition ) Then
Beep
Cancel = True
Exit Sub
End If
Exit Sub
'-----------------------------------------------------
Private Sub TextGrp2_Change(ByVal Index As Integer) 'TextBox51-100
'Any processing for TextGrp2.Item(Index)
End Sub
Private Sub TextGrp2_OnExit(ByVal Index As Integer, _
ByVal Cancel As MSForms.ReturnBoolean) 'TextBox51-100
If ( Any invalid condition ) Then
Beep
Cancel = True
Exit Sub
End IF
Exit Sub