How do we know the sender of the buttons in VBA?

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

Good day. I would like to find out the sender of many buttons.

Something like that in 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); } 

I don't have any idea in VBA (not VB!). VBA means Visual Basic for Applications. It is possible in VBA? I'm using Excel. I've tried Application.Caller, but it doesn't work. Any samples - suggestions?

回答1:

You have to wire up the button events. You can do so in the Worksheet Activate event handler:

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 

Then make a Class Module named ButtonEvents with this code:

Public WithEvents cmdButton As MSForms.CommandButton  Private Sub cmdButton_Click()  MsgBox cmdButton.Caption & " was pressed!" End Sub 


回答2:

Rename your Shapes.Name values to something you can use however you need.

When you create a group of shapes in Excel, lets say rectangles, right click the first object then go to the Formulas tab and click "Define Name". At the bottom of the pup-up window will be a line: Refers to [ ="Rectangle 73" ] This is your VBA Shapes.Name value. (Doing anything here will not change the VBA Shapes.Name value)

You can use the individual object name with IF Application.Caller.Name = "Rectangle 73" or you can rename your shaped in VBA to something more useful.

If you manually created each shape, they will be "Rectangle 73", "Rectangle 74", "Rectangle 75"... If you copied and pasted, they will be "Rectangle 73", "Rectangle 102", "Rectangle 103"... Now that we have our Shapes.Name values we can redefine them as needed in 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 

Place this code in its own separate module, then simply enter the values and click the play button in the VBA window toolbar for RunSub.

Now, you can use n = Right(Application.Caller, 2) to get the number value of the button clicked. Be sure to define Dim n As Integer.

If different button groups call the same function, you can use Select Case Left(Application.Caller, 7) to get the first 7 letters of the shape.name and Case "newName" for actions specific to that button group.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!