2016-06-27 11 views
2

a イベントをクリックします。しかし、VBAでは、フォームをコントロールしやすくし、コーディングしていない人に使ってもらいたいので、コードを1つ1つ作成したくないので59 Subsをクリックします。はかなり醜いでしょう。私はVBAコードで別のサブを作成する必要がある以上を追加したい。自動生成テキストボックス

本当の質問は:さまざまな変数を持つテキスト/ボタンごとに自動コード(サブ)を実行する方法はありますか?

EG:

Text1_Click + Code -> Variable: 1 
Text2_Click + Code -> Variable: 2 
Text3_Click + Code -> Variable: 3 --> Text(?)_Click + Code -> Variable: ? 
Text4_Click + Code -> Variable: 4 
Text5_Click + Code -> Variable: 5 
+1

可能な複製を設定する必要がある場合(http://stackoverflow.com/質問/ 17888875/items-with-buttons) –

+0

イベントシンクを使用すると、コントロールから継承したコードを基本的に作成してから、イベントを変更する機能を使用できます( –

+0

)コントロールアレイ](https://msdn.microsoft.com/en-us/library/aa241305(v = 60).aspx)を参照してください。これにより、1つのサブが多くのコントロールを処理できます。 –

答えて

2

は、VBA関数を作成し、各テキストボックスのクリック時プロパティのためにそれを使用する:あなたの例では=MyFunction()

、テキストボックスText1Text5という名前で、Variableをcテキストボックス名から番号を取得します。その番号はMid()で簡単に抽出できます。

Public Function MyFunction() 
    Dim strControl As String 
    Dim Variable As Variant 

    strControl = Application.Screen.ActiveControl.Name 
    Variable = Null 
    If strControl Like "Text*" Then 
     Variable = Val(Mid(strControl, 5)) 
    End If 
    Debug.Print strControl & "_Click -> Variable: " & Nz(Variable, "Null") 
End Function 

あなたは番号のコマンドボタンにそのアプローチを拡張したい場合は、これを含めます...

If strControl Like "Command*" Then 
     Variable = Val(Mid(strControl, 8)) 
    End If 

あなたは、プログラムでクリックでプロパティを割り当てるコントロールをループし、[ボタンでアイテムの配列]の各.OnClick = "=MyFunction()"

+0

これは完璧に機能していて使いやすいです。ありがとうございます。 –

0

はい、あなたがコントロールを参照することができます!その後、CTL私を参照します

n = 5 
Set ctl = Me("Text" & CStr(n)) 

Text5

しかし、WithEvents 0を見る:How to write generic code ...

+0

ありがとう、私はコントロールを参照することができますが、どのようにボタンのクリックにそれを割り当てるかわかります。 (私はちょっとこの言語を学んでいます^^) –

+0

Nathanのように_WithEvents_に行きましょう。 – Gustav

1

フォームでは、ボタンを持っているし、次のコード

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 
+0

これは私にこの行にタイプマッチエラーを与えます: clsCustom.INITIALISE ctl –

+0

あなたが渡しているコマンドボタンなのでしょうか?あなたはカスタム –

+0

に変更する前にコントロールのtypenameをチェックする必要があります(私はこの言語でちょっと新鮮です)。私はそれを修正することができましたが、最後の2つのコードはどこに置くのですか?とにかくそれを見つけることができない場合は、私はあなたが私のプロジェクトを与えて、それらの名前と機能/クラスで私を導くことができます。私は本当にそれと混同しています。 –