从用户表单按钮传递字符串单击到 Excel VBA
Pass a string from a User Form Button Click to Excel VBA
我创建了一个用户窗体,它获取打开的 PowerPoint 文件列表并让用户 select 他们想要使用哪个文件。当他们单击 'Set PowerPoint' 按钮时,我想将该按钮传递给 VBA 模块,我将在其中将 PowerPoint 设置为用户刚刚 select 编辑的那个。任何帮助将不胜感激。
用户表单代码:
Option Explicit
Public SelectedPPT As String
Private Sub cmdCloseForm_Click()
Unload Me
End Sub
Private Sub ComboBox1_Change()
ComboBox1.RowSource = "Array"
End Sub
Private Sub setPPT_Click()
'Not sure if this is the best way to select the ppt the user has chosen?
SelectedPPT = Me.ComboBox1.Value
End Sub
用户窗体如下所示:
那么我如何获取 SelectedPPT 并将其传递给模块以便我可以 select 特定的 PowerPoint?
您的 SelectedPPT
破坏了 封装 并且可以从表单外部进行设置,而不仅仅是表单的代码。您可以通过创建字段 Private
并为其公开 Property Get
过程来缓解此设计问题:
Option Explicit
Private SelectedPPT As String
Public Property Get SelectedFile() As String
SelectedFile = SelectedPPT
End Property
在表单的 Initialize
或 Activate
处理程序中设置 ComboBox
行源,因此它被初始化一次。然后在 ComboBox 中的选择更改时分配 SelectedPPT
- 这是您的 ComboBox1_Change
处理程序。
这样就不再需要 [Set PowerPoint] 按钮及其 Click
处理程序。
我会有一个 [确定] 和一个 [取消] 按钮,我会让表单记住它是否被取消:
Private IsCancelled As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = IsCancelled
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
IsCancelled = True
Me.Hide
End Sub
然后您还需要考虑用户单击红色 X 按钮关闭表单的情况;您可以通过处理表单的 QueryClose
事件来做到这一点:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
IsCancelled = True
Cancel = True
Me.Hide
End If
End Sub
其余逻辑属于调用代码 - 假设表单被调用 MyAwesomeForm
;你会有这样的东西:
Dim filename As String
With New MyAwesomeForm
.Show
If Not .Cancelled Then
filename = .SelectedFile
'do whatever you wanted to do with that filename
End If
End With
注:
- 表单绝不会调用
Unload Me
- 调用者有责任创建和销毁表单对象
- 调用者每次都使用一个
New
表单实例
- 调用者对表单通过属性公开的任何值具有只读访问权限
我创建了一个用户窗体,它获取打开的 PowerPoint 文件列表并让用户 select 他们想要使用哪个文件。当他们单击 'Set PowerPoint' 按钮时,我想将该按钮传递给 VBA 模块,我将在其中将 PowerPoint 设置为用户刚刚 select 编辑的那个。任何帮助将不胜感激。
用户表单代码:
Option Explicit
Public SelectedPPT As String
Private Sub cmdCloseForm_Click()
Unload Me
End Sub
Private Sub ComboBox1_Change()
ComboBox1.RowSource = "Array"
End Sub
Private Sub setPPT_Click()
'Not sure if this is the best way to select the ppt the user has chosen?
SelectedPPT = Me.ComboBox1.Value
End Sub
用户窗体如下所示:
那么我如何获取 SelectedPPT 并将其传递给模块以便我可以 select 特定的 PowerPoint?
您的 SelectedPPT
破坏了 封装 并且可以从表单外部进行设置,而不仅仅是表单的代码。您可以通过创建字段 Private
并为其公开 Property Get
过程来缓解此设计问题:
Option Explicit
Private SelectedPPT As String
Public Property Get SelectedFile() As String
SelectedFile = SelectedPPT
End Property
在表单的 Initialize
或 Activate
处理程序中设置 ComboBox
行源,因此它被初始化一次。然后在 ComboBox 中的选择更改时分配 SelectedPPT
- 这是您的 ComboBox1_Change
处理程序。
这样就不再需要 [Set PowerPoint] 按钮及其 Click
处理程序。
我会有一个 [确定] 和一个 [取消] 按钮,我会让表单记住它是否被取消:
Private IsCancelled As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = IsCancelled
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
IsCancelled = True
Me.Hide
End Sub
然后您还需要考虑用户单击红色 X 按钮关闭表单的情况;您可以通过处理表单的 QueryClose
事件来做到这一点:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
IsCancelled = True
Cancel = True
Me.Hide
End If
End Sub
其余逻辑属于调用代码 - 假设表单被调用 MyAwesomeForm
;你会有这样的东西:
Dim filename As String
With New MyAwesomeForm
.Show
If Not .Cancelled Then
filename = .SelectedFile
'do whatever you wanted to do with that filename
End If
End With
注:
- 表单绝不会调用
Unload Me
- 调用者有责任创建和销毁表单对象
- 调用者每次都使用一个
New
表单实例 - 调用者对表单通过属性公开的任何值具有只读访问权限