【问题标题】:Get corresponding Range for Button interface object获取Button接口对象对应的Range
【发布时间】:2017-01-06 11:21:07
【问题描述】:

我希望当我单击“Sélectionner un poste”按钮时,它会告诉我位置。 (我在哪一行点击的按钮。)

创建按钮的代码:

Sub AjouterBoutonPoste(positionX As Integer, positionY As Integer, nom As String)
    Set t = ActiveSheet.Range(Cells(positionX, positionY), Cells(positionX, positionY))
    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
    With btn
        .OnAction = "PosteBtAction"
        .Caption = "Sélectionner un poste"
        .Name = nom & CStr(positionX) & CStr(positionY)
    End With
End Sub

事件按钮的代码:

Sub PosteBtAction()
   AssocierSessoinCandidature.Show
End Sub

我有一个名为AssocierSessoinCandidature 的应用程序窗口。我希望将我点击的位置发送到应用程序窗口。

这是我的 Excel 表格示例:

【问题讨论】:

    标签: vba excel button


    【解决方案1】:

    我用过非常相似的东西,你可以修改下面的代码:

    Sub Button24_Click()
    Dim strShape As String
    strShape = Shapes("button24").TopLeftCell.Address
    CallAnotherFunction strShape
    End Sub
    

    单击按钮后,此代码会将 button24 的范围作为字符串(不知道为什么我不使用范围,如果您愿意,可以)并将其传递给函数CallAnotherFunction

    总之,这就是你所需要的:

    Shapes("button24").TopLeftCell.Address
    

    Shapes("button24").TopLeftCell.Row
    

    【讨论】:

      【解决方案2】:

      点击按钮时调用下面的Sub

      Sub foo()
      
      Dim obj As Object
      Dim row_no As Integer
      
      Set obj = ActiveSheet.Buttons(Application.Caller)
      With obj.TopLeftCell
          row_no = .Row
      End With
      MsgBox "The Button is in the row number " & row_no
      
      End Sub
      

      【讨论】:

        【解决方案3】:

        您可以访问TopLeftCellBottomRightCellButton 对象的属性以获取绑定控件的范围地址:

        Option Explicit
        
        Sub Test()
        
            Dim ws As Worksheet
            Dim btn As Object
            Dim strAddressTopLeft As String
            Dim strAddressBottomRight As String
            Dim strButtonName As String
        
            Set ws = ActiveSheet
        
            For Each btn In ws.Buttons
                strAddressTopLeft = btn.TopLeftCell.Address
                strAddressBottomRight = btn.BottomRightCell.Address
                strButtonName = btn.Name
                Debug.Print "Range of button (" & strButtonName & "): " & _
                    strAddressTopLeft & ":" & _
                    strAddressBottomRight
            Next btn
        
        End Sub
        

        【讨论】:

          猜你喜欢
          • 2021-07-10
          • 1970-01-01
          • 1970-01-01
          • 2020-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-17
          相关资源
          最近更新 更多