【问题标题】:How can I run a userform from a macro?如何从宏运行用户窗体?
【发布时间】:2019-02-04 04:20:37
【问题描述】:

我在 VBA for Excel 2016 中开发了许多 UDF 和宏。我的一个宏使用输入框来获取宏随后使用的数据。我想用用户表单替换输入框。我创建了带有一个文本框的用户表单。我想激活用户表单,用默认数据填充文本框,并在选择 OK 时将文本框数据返回给宏。我已经广泛搜索了执行此操作所需的所有代码的端到端示例,但没有运气。这个简单问题的例子是否存在?

【问题讨论】:

  • 要打开用户表单,请使用userformname.show。查找用户窗体打开时执行的代码的 Userform_Initialize 事件。在这个子里你可以写你想要发生的事情,比如填写默认数据。
  • 研究Private Sub UserForm_Initialize的使用......正是这个例程将保存您的文本框默认值分配代码(如Me.myTextBox.value = "test"
  • Private Sub btnOK_Click()(将 btnOK 替换为 OK 按钮的实际名称)将保存将 Me.myTextBox 的值移动到您选择的单元格的代码。
  • 我尝试使用 userform_Initialize 子。它没有设置初始值。然后我尝试在调用 .show 之前立即设置它;那行得通。抱歉,我会附上代码,但我看不出有什么办法。
  • 将您的代码添加到您的原始问题(不在评论中)。添加您的代码,选择它,然后单击工具栏中的{}。或者,如果只有几行,请将代码行缩进 4 个空格。网站上的某处有问题发布说明。

标签: excel vba userform


【解决方案1】:

将属性添加到您的用户表单。对于这个答案,让我们在用户表单中使用以下代码。

Public Property Get MyResult() As String
    ' You may want to do any manipulation here
    ' including converting to a number, in which case the return type should be changed (*)
    MyResult = TextBox1.Text
End Property

(*) 如果您正在进行转换,您可以在您的用户表单中设置另一个功能来禁用“确定”按钮,直到他们在文本框中有有效的可转换数据。

您还想知道他们是否点击了“取消”

Public Property Get Cancelled() As Boolean
    Cancelled = pCancelled ' Declare pCancelled as a Boolean in the scope of the form
End Property

Public Sub CancelButton_Click() ' Standard click event for the button
    pCancelled = True
    Me.Hide
End Sub

Public Sub OKButton_Click() ' Standard click event for the button
    pCancelled = False
    Me.Hide
End Sub

在你的调用宏中

MyForm.Show ' This is modal, so will block execution until a response is provided
If Not MyForm.Cancelled Then
    Debug.Print MyForm.MyResult
    'Do something with MyForm.MyResult
End If
UnLoad MyForm ' assuming you do not want to re-use this form as part of your logic.

【讨论】:

    【解决方案2】:

    有一个示例说明如何将值传递给表单并返回结果。该方法使用在标准模块范围内创建的Scripting.Dictionary 对象并传递给用户表单以允许更改值。因此,可以将默认值发送给用户表单,并在用户表单关闭和卸载后将结果值保留在字典中。您可能有多个值,只需将必要数量的键添加到字典中,例如。 G。 oData("property1")oData("property2")

    在项目中添加一个标准模块并将以下代码放入其中:

    Option Explicit
    
    Sub Test()
    
        Dim oData
    
        ' Set default value and show form
        Set oData = CreateObject("Scripting.Dictionary")
        oData("") = "Some default text"
        UserForm1.ShowForm oData
        ' Wait until user close form
        Do While IsUserFormLoaded("UserForm1")
            DoEvents
        Loop
        ' Output returned value
        MsgBox oData("")
    
    End Sub
    
    Function IsUserFormLoaded(UserFormName As String) As Boolean
    
        Dim oUF As Object
    
        For Each oUF In UserForms
            If LCase(oUF.Name) = LCase(UserFormName) Then
                IsUserFormLoaded = True
                Exit Function
            End If
        Next
    
    End Function
    

    在项目中添加一个名为UserForm1的用户表单模块,放置控件如图:

    并将以下代码放入用户表单模块:

    Private opData
    
    Public Sub ShowForm(oData)
    
        Set opData = oData
        Me.TextBox1.Value = opData("")
        Me.Show
    
    End Sub
    
    Private Sub UserForm_Initialize()
    
        If TypeName(opData) <> "Dictionary" Then Set opData = CreateObject("Scripting.Dictionary")
    
    End Sub
    
    Private Sub CommandButton1_Click()
    
        Unload Me
    
    End Sub
    
    Private Sub CommandButton2_Click()
    
        opData("") = Me.TextBox1.Value
        Unload Me
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多