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