获取从中调用Click-Method的CommandButton Number

时间:2018-03-12 11:53:54

标签: vba excel-vba excel

我在Userform中创建了一堆带有我的类clsComamndButtons的Commandbuttons。到目前为止一切都有效。所有这些按钮都应该这样做,但我不知道如何获得我称之为cmdCommandButton_Click方法的Button的名称。我想在不同的单元格中编写选择文件夹的路径,具体取决于单击的按钮。

这是我的班级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

2 个答案:

答案 0 :(得分:1)

使用.name属性cmdCommandButton.name

答案 1 :(得分:0)

一般情况下,要获取按钮的行,请使用BottomRightCell属性或TopLeftCell,具体取决于您所需要的内容。

如果您正在使用ActiveX按钮(通常建议),那么这样的事情就可以了:

Private Sub CommandButton1_Click()
    Debug.Print CommandButton1.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