0

たくさんのボタンの差出人を知りたいです。

C#のようなもの:

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);
}

VBA (Visual Basic for Applications)で可能ですか?エクセルを使用しています。Application.Callerを試しました。

4

2 に答える 2

7

ボタンイベントを接続する必要があります。これは、Worksheet 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
于 2012-04-12T20:07:37.923 に答える
0

Shapes.Name 値の名前を、必要に応じて使用できる名前に変更します。

Excel で図形のグループを作成する場合、たとえば四角形を例にとると、最初のオブジェクトを右クリックし、[数式] タブに移動して [名前の定義] をクリックします。pup-up ウィンドウの下部に次の行があります。 [ ="Rectangle 73" ] を参照 これは VBA Shapes.Name 値です。(ここで何かを行っても、VBA Shapes.Name の値は変更されません)

個々のオブジェクト名を使用することもIF Application.Caller.Name = "Rectangle 73"、VBA で整形した名前をより便利なものに変更することもできます。

それぞれの形を手で作ると「Rectangle 73」「Rectangle 74」「Rectangle 75」・・・コピペすると「Rectangle 73」「Rectangle 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 の最初の 7 文字を取得するために使用し、Case "newName"そのボタン グループに固有のアクションを実行するために使用できます。

于 2013-10-05T17:55:46.663 に答える