フォームでは、ボタンを持っているし、次のコード
Option Explicit
Public colCustomControls As Collection
Private Sub Form_Open(Cancel As Integer)
Dim ctl As Control
Dim clsCustom As clsCustomButton
Set colCustomControls = New Collection
For Each ctl In Me.Controls
Set clsCustom = New clsCustomButton
clsCustom.INITIALISE ctl
colCustomControls.Add clsCustom
Next ctl
End Sub
何が起こっている
Option Explicit
Private WithEvents cbCustom As CommandButton
Public Sub INITIALISE(cb As CommandButton)
Set cbCustom = cb
cb.OnClick = "[Event Procedure]"
End Sub
Private Sub cbCustom_Click()
Select Case cbCustom.Name
Case "Command0": MsgBox "Button1 clicked"
Case "Command1": MsgBox "Button2 clicked"
End Select
End Sub
clsCustomButton
と呼ばれるクラスは、コレクションには、me.Controlsを模倣されていますコレクションをフォームに追加しますが、すべてのボタンをクラスにするので、クラス_clickイベントを使用しています。サブ呼び出しの場合は、初期化を変更してproc nameイベントを取得し、application.Runを使用してコードを呼び出すこともできます。
ようなので、
Option Explicit
Private WithEvents cbCustom As CommandButton
Private strProcCall As String
Public Sub INITIALISE(cb As CommandButton, strProc As String)
Set cbCustom = cb
strProcCall = strProc
cb.OnClick = "[Event Procedure]"
End Sub
Private Sub cbCustom_Click()
Application.Run strProcCall
End Sub
と
Option Explicit
Public colCustomControls As Collection
Private Sub Form_Open(Cancel As Integer)
Dim ctl As Control
Dim clsCustom As clsCustomButton
Set colCustomControls = New Collection
Set clsCustom = New clsCustomButton
clsCustom.INITIALISE Me.Controls("Command0"), "MACRO_1"
colCustomControls.Add clsCustom
Set clsCustom = New clsCustomButton
clsCustom.INITIALISE Me.Controls("Command1"), "MACRO_2"
colCustomControls.Add clsCustom
End Sub
可能な複製を設定する必要がある場合(http://stackoverflow.com/質問/ 17888875/items-with-buttons) –
イベントシンクを使用すると、コントロールから継承したコードを基本的に作成してから、イベントを変更する機能を使用できます( –
)コントロールアレイ](https://msdn.microsoft.com/en-us/library/aa241305(v = 60).aspx)を参照してください。これにより、1つのサブが多くのコントロールを処理できます。 –