Private UserForm_Initialize()
Set colWeekBtn = New Collection ' インスタンスの生成
With colWeekBtn .Add cmdSun .Add cmdMon .Add cmdTue
: .Add cmdSat
End With
End Sub
Private Sub cmdWeek_Click_Sub(ByVal Index As Integer)
Dim vntWeekName As Variant
vntWeekName = Array("", "日", "月", "火",
"水", "木", "金", "土")
MsgBox vntWeekName(Index) & "曜日ボタンがクリックされました(" & Index & ")"
If (colWeekBtn(Index).BackColor = vbButtonFace) Then
colWeekBtn(Index).BackColor = vbRed
Else
colWeekBtn(Index).BackColor = vbButtonFace
End If
End Sub
> Private Sub cmdSun_Click() もしくは Private
Sub cmdWeek1_Click()
> Call cmdWeek_Click_Sub(1)
> End Sub
>
> Private Sub cmdMon_Click()
> Call cmdWeek_Click_Sub(2)
> End Sub
> :
> :
> Private Sub cmdSat_Click()
> Call cmdWeek_Click_Sub(7)
> End Sub
Private cmdWeekBtn(1 To 7) As MsForms.CommandButton
Private UserForm_Initialize() SetcmdWeekBtn(1) = cmdSun SetcmdWeekBtn(2) = cmdMon
:
: SetcmdWeekBtn(7) = cmdSat
End Sub
もしくは、
Private UserForm_Initialize()
Dim i As Integer
For i = 1 To 7 SetcmdWeekBtn(i) = Controls("cmdWeek" & i)
Next i
End Sub
Private Sub cmdWeek_Click_Sub(ByVal Index As Integer)
Dim vntWeekName As Variant
vntWeekName = Array("", "日", "月", "火",
"水", "木", "金", "土")
MsgBox vntWeekName(Index) & "曜日ボタンがクリックされました(" & Index & ")"
If (cmdWeekBtn(Index).BackColor = vbButtonFace) Then
cmdWeekBtn(Index).BackColor = vbRed
Else
cmdWeekBtn(Index).BackColor = vbButtonFace
End If
End Sub