【问题标题】:VBA pass argument to userformVBA将参数传递给用户窗体
【发布时间】:2015-06-30 15:32:01
【问题描述】:

我有一个用户表单,我希望将一个范围传递给它。我尝试了几种不同的方法,但似乎都不起作用。

下面是子代码:

Option Explicit

Sub Additional_Comments_Normal()
Dim MSG1 As Integer
Dim msg As String
Dim act As Range


On Error GoTo ErrHandler

'Calls userform

MSG1 = MsgBox("Would you like to add comments", vbYesNo, "Add comments")


If MSG1 = vbYes Then

With AddComments
On Error Resume Next
Set act = Application.InputBox(Prompt:="Please choose files you wish to add comments to", Type:=8)

If act Is Nothing Then
Exit Sub
End If
    
    Application.ScreenUpdating = True
    .Show
    
End With

Else
Exit Sub
End If

ErrHandler:

If Err.Number <> 0 Then
 msg = "Error # " & Str(Err.Number) & " was generated by " _
         & Err.Source & Chr(13) & Err.Description
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
End If
End Sub

用户表单代码在这里:

Public act As Range

Private Sub CommandButton1_Click()

Dim ctl As Control
Dim rng As Range
Dim MSG2 As Integer
Dim sfile As String


    If act.Column > 1 Then
        MsgBox ("Please choose File name from Column 1")
    Exit Sub
    End If

        If act.Row < 4 Then
            MsgBox ("Please choose a valid file")
        Exit Sub
        End If
         
            If Me.TxtComment.Value = "" Then
                MsgBox "Please add comments", vbExclamation, "Additional Comments"
                Me.TxtComment.SetFocus
            Exit Sub
            End If

            If Me.TxtName.Value = "" Then
                MsgBox "Please add your name", vbExclamation, "Additional Comments"
                Me.TxtName.SetFocus
            Exit Sub
            End If



MSG1 = MsgBox("Add Comments ?", vbYesNo, "Add comments")

If MSG1 = vbNo Then

End If

If MSG1 = vbYes Then

act.Offset(0, 16).Value = act.Offset(0, 16).Text & " " & Me.TxtComment.Value
act.Offset(0, 17).Value = act.Offset(0, 17).Text & " " & Me.TxtName.Value
 
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
ctl.Value = ""
End If
Next ctl
      
AddComments.Hide

Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True

End If

End Sub


Private Sub CommandButton2_Click()

End Sub

Private Sub CommandButton3_Click()
Unload Me
End Sub

Private Sub UserForm_Click()

End Sub

然后我收到关于未定义变量的行为的错误。

任何人都可以阐明更好的流程吗?

【问题讨论】:

    标签: excel arguments userform vba


    【解决方案1】:

    您在代码顶部设置了Option Explicit。这意味着需要定义所有变量(这被认为是良好的编程习惯)。因此,您有两种选择来解决此问题:

    (1) 从您的代码中删除 Option Explicit 行或

    (2) 使用Dim 命令定义所有变量。在这种情况下,您必须在表单上将 Dim act as Range 添加到您的 Sub CommandButton1_Click

    如果您想将变量传递给另一个子,那么您可以使用该变量调用该子,如下所示:

    Call Additional_Comments_Normal(act)

    并且子标题需要像这样改变:

    Sub Additional_Comments_Normal(ByVal act as Range)
    
    '(your code)'
    
    End Sub
    

    如果“将变量传递给另一个子”太麻烦,那么您也可以将范围保存在文件中的某个位置,如下所示:

    SomeHiddenSheet.Range("A1").Value2 = act
    

    在另一个子中你可以再次发起行动:

    act = SomeHiddenSheet.Range("A1").Value2
    

    【讨论】:

    • 我选择了选项 2,这会引发错误 91,它无法识别我在 sub 中定义了 Act。
    • 您没有在 sub 中定义它,而是在外部定义它。仍然应该工作。
    • 我正在尝试将范围行为传递给用户窗体,因此调用过程的工作方式不同?
    • Excel 怎么知道act 是什么,除非你告诉它你想要它是什么。如果您想知道各种变量的范围,那么您可能需要考虑阅读以下内容:support.microsoft.com/en-en/kb/141693/en
    • 嗨拉尔夫,这正是我要问你的问题在您使用 With Userform 命令的地方,然后 Public 声明我在网上看到的文献中显然有效的变量,这种方法对我不起作用,因此我在这里询问。所以我的问题本质上是问我在这个脚本中做错了什么。 ozgrid.com/forum/showthread.php?t=147329
    猜你喜欢
    • 2020-04-04
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多