Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取单击按钮的行号的VBA代码_Vba_Button_Row - Fatal编程技术网

获取单击按钮的行号的VBA代码

获取单击按钮的行号的VBA代码,vba,button,row,Vba,Button,Row,我正在尝试自动确定已单击命令按钮的行 我正在使用以下代码: Private Sub CommandButton2_Click () Dim b As Object, r As Integer Set b = ActiveSheet.Buttons(Application.Caller) With b.TopLeftCell r= .row End With MsgBox "Row Number " &a

我正在尝试自动确定已单击命令按钮的行

我正在使用以下代码:

Private Sub CommandButton2_Click () 
   
    Dim b As Object, r As Integer 
    Set b = ActiveSheet.Buttons(Application.Caller) 
    With b.TopLeftCell 
        r= .row
    End With 
    MsgBox "Row Number " & r
End Sub 
但是我在运行代码时不断收到一条错误消息,上面写着“运行时错误1004-无法获取工作表类的Buttons属性”


您能提供建议吗?

作为@BigBen notes,您可能有一个ActiveX按钮,因此您可以使用:

Private子命令按钮2\u单击()
MsgBox Me.CommandButton2.TopLeftCell.Row
端接头

按钮
用于表单控件。你可能有一个ActiveX控件。你是对的,我使用的是活动的x按钮。有没有办法使所有命令按钮的代码都是动态的?如果您有很多ActiveX按钮,并且不想为每个按钮都编写处理程序,那么您可以使用这种方法:您可能会发现使用表单按钮(或者只是形状)和单个“OnAction”更容易sub使用
应用程序。调用者
方法。感谢工作人员:)