2012-04-12 7 views
0

おやすみなさい。 私は送信者多くのボタンを探してみたいと思います。VBAのボタンの送信者をどのように知っていますか?

C#のでそのような何か:私はVBAで任意のアイデアを持っていない

Button b = new Button(); 
b.Text = "Click 1"; 
b.Location = new Point(20, 20); 
b.Size = new Size(100, 20); 
// Our Handler 
b.Click += new EventHandler(myClick); 

// Implementation of next button... 

// Add to form 
this.Controls.Add(b); 
this.Controls.Add(nextButton); 
... 

// And our event 
private void myClick(object sender, System.EventArgs e) 
{ 
    var mysender = ((Button)sender).Name; 
    doAnything(mysender); 
} 

(VBではありません!)。 VBAはVisual Basic for Applicationsを意味します。 VBAでも可能ですか?私はExcelを使用しています。私はApplication.Callerを試しましたが、動作しません。どのサンプル - 提案ですか?

+1

どのようなタイプのボタン(フォーム?アクティブなの?)とはどこですか(フォーム上、ワークシート上にありますか?)どのVBAコードを試しましたか?これは動作させることができますが、あなたがしたいことについて正確に詳細を提供する必要があります... –

答えて

4

ボタンイベントを配線する必要があります。あなたは、ワークシートのActivateイベントハンドラで行うことができます。

Dim arrEvents As Collection 

Private Sub Worksheet_Activate() 

Dim objButtonEvents As ButtonEvents 
Dim shp As Shape 

Set arrEvents = New Collection 

For Each shpCursor In Me.Shapes 
    If shpCursor.Type = msoOLEControlObject Then 
     If TypeOf shpCursor.OLEFormat.Object.Object Is MSForms.CommandButton Then 
      Set objButtonEvents = New ButtonEvents 
      Set objButtonEvents.cmdButton = shpCursor.OLEFormat.Object.Object 
      arrEvents.Add objButtonEvents 
     End If 
    End If 
Next 

End Sub 

その後、クラスモジュールは、このコードでButtonEventsを命名します:あなたが必要とするしかし

Public WithEvents cmdButton As MSForms.CommandButton 

Private Sub cmdButton_Click() 
MsgBox cmdButton.Caption & " was pressed!" 
End Sub 
0

は、あなたが使用できる何かにあなたのShapes.Name値の名前を変更します。

Excelで図形のグループを作成する場合は、四角形とし、最初のオブジェクトを右クリックして[式]タブに移動し、[名前の定義]をクリックします。 pup-upウィンドウの一番下には、次のような行が表示されます。[= "Rectangle 73"]これはVBAのShapes.Name値です。 (ここでは何もしないとVBA Shapes.Nameの値は変わりません)

個々のオブジェクト名はIF Application.Caller.Name = "Rectangle 73"とすることができます。また、VBAで整形した名前を変更することもできます。

それぞれの図形を手動で作成した場合、「長方形73」、「長方形74」、「長方形75」...コピーして貼り付けると、「長方形73」、「長方形102」、 "Rectangle 103" ... Shapes.Name値があるので、VBAで必要に応じてそれらを再定義できます。独自の個別のモジュールでこのコード

Sub nameShapes() 
    Dim shp As String 
    Dim shpType As String 
    Dim myName As String 

'original root name of group shapes *do not include trailing space* 
    shpType = "Rectangle" 

'new name of group shapes 
    myName = "newName" 

'first number value of new shape names 
    n = 1 

'number values of first, second, and last shapes 
    n1 = 73 
    n2 = 103 
    nx = 124 

'active code -------------------------------------- 
'-------------------------------------------------- 
    shp = shpType & " " & n1 
    ActiveSheet.Shapes(shp).Name = myName & " " & n 

    For x = n2 To nx 
     n = n + 1 
     shp = shpType & " " & x 
     ActiveSheet.Shapes(shp).Name = myName & " " & n 
    Next n 
'-------------------------------------------------- 
'active code -------------------------------------- 
End Sub 

プレイスには、単に値を入力し、RunSubのためのVBAウィンドウのツールバーにある再生ボタンをクリックしてください。

ここでn = Right(Application.Caller, 2)を使用して、クリックしたボタンの数値を取得できます。 Dim n As Integerを必ず指定してください。

異なるボタングループが同じ機能を呼び出す場合は、Select Case Left(Application.Caller, 7)を使用して、そのボタングループに固有のアクションのshape.nameおよびCase "newName"の最初の7文字を取得できます。

関連する問題