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

前端 未结 2 554
醉梦人生
醉梦人生 2020-12-07 06:54

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

Something like that in C#:

Button b = new Bu         


        
相关标签:
2条回答
  • 2020-12-07 07:10

    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.

    0 讨论(0)
  • 2020-12-07 07:23

    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
    
    0 讨论(0)
提交回复
热议问题