Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 获取调用Click方法的CommandButton编号_Vba_Excel - Fatal编程技术网

Vba 获取调用Click方法的CommandButton编号

Vba 获取调用Click方法的CommandButton编号,vba,excel,Vba,Excel,我已经用我的类clsComamndButtons在Userform中创建了一组Commandbuttons。到目前为止一切正常。所有这些按钮都应该执行相同的操作,但我不知道如何获取调用cmdCommandButton\u Click方法的按钮的名称。我想在不同的单元格中写入choosen文件夹的路径,具体取决于单击的按钮 这是我的类clsCommandButtons: Option Explicit Public WithEvents cmdCommandButton As MSForms.C

我已经用我的类clsComamndButtons在Userform中创建了一组Commandbuttons。到目前为止一切正常。所有这些按钮都应该执行相同的操作,但我不知道如何获取调用cmdCommandButton\u Click方法的按钮的名称。我想在不同的单元格中写入choosen文件夹的路径,具体取决于单击的按钮

这是我的类clsCommandButtons:

Option Explicit

Public WithEvents cmdCommandButton As MSForms.CommandButton
Private msOnAction As String
Private mobjParent As Object

Public Property Get Object() As MSForms.CommandButton
    Set Object = cmdCommandButton
End Property

Public Function Load(ByVal parentFormName As Object, ByVal btn As MSForms.CommandButton, ByVal procedure As String) As clsCommandButtons
    Set mobjParent = parentFormName
    Set cmdCommandButton = btn
    msOnAction = procedure
    Set Load = Me
End Function

Private Sub Class_Terminate()
    Set mobjParent = Nothing
    Set cmdCommandButton = Nothing
End Sub


Private Sub cmdCommandButton_Click()
    Dim sFilepath       As String                       'Pfad der gewählten .txt-Filterdatei

    'Datei öffnen - Dialog
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .InitialFileName = ActiveWorkbook.Path & "\"
        .Filters.Add "TextFiles", "*.txt", 1
        .FilterIndex = 1
        If .Show = -1 Then
            sFilepath = .SelectedItems(1)
        End If
    End With

    Cells(Row+???Depening on the Button which was clicked), constClmn) = sFilepath
End Sub

使用
.name
属性
cmdCommandButton.name

通常,要获取按钮的行,请根据需要使用
BottomRightCell
属性或
TopLeftCell

如果您使用的是ActiveX按钮(一般建议),那么这样的按钮应该可以:



如果您使用的是表单元素,则将按钮事件分配给该元素,请参见:

Public Sub GetTheButtonName()
    Debug.Print "My name is ... "
    Debug.Print Buttons(Application.Caller).Name
    Debug.Print Buttons(Application.Caller).BottomRightCell.Row
End Sub
Public Sub GetTheButtonName()
    Debug.Print "My name is ... "
    Debug.Print Buttons(Application.Caller).Name
    Debug.Print Buttons(Application.Caller).BottomRightCell.Row
End Sub