调用Sub后传递变量

时间:2017-01-04 12:59:32

标签: excel vba excel-vba

我有两个问题。

  1. 因为Var3有时可能是空白的,取决于Sub,我得到一个424错误,并且单词' undefined'被复制到网站上的文本框中 - 但是我需要将其留空并且在这种情况下不粘贴任何内容。

  2. 我在Excel中有一个下拉框,用于选择要调用的Sub。我想将其合并到IF语句中,如下所示,但是当我尝试代码时,没有任何反应。

  3. 这是我的尝试:

    Public Sub Populate()
    
    Dim Var1 As String
    Dim Var2 As String
    Dim Var3 As String    
    
    Dim User_Name As String
    Dim StrFile1 As String
    Dim StrFile2 As String
    Dim Filename As String
    Dim strFilename As String
    
    Dim IE As Object
    
    If ComboBox1.Value = "Data1" Then
        Call Data1 (Filename, Var1, Var2, Var3)
    End If
    
    If ComboBox1.Value = "Data2" Then
        Call Data2 (Filename, Var1, Var2, Var3)
    End If
    
    'If ComboBox1.Value = Data3, Data4, etc...
    
        User_Name = Environ("UserName")
        StrFile1 = "C:\Users\"
        StrFile2 = "\Desktop\"
    
        strFilename = StrFile1 & User_Name & StrFile2 & Filename
    
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & strFilename, Destination _
            :=Range("$A$22"))
            .Name = Filename
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = False
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    
        ActiveWindow.SmallScroll Down:=-27
    
        DoEvents
    
        'FillInternetForm
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Navigate "https://website.com"
        IE.Visible = True
        While IE.busy
            DoEvents  'wait until IE is done loading page.
        Wend
    
        IE.Document.All(Var1).Value = ThisWorkbook.Sheets("Sheet1").Range("B5")
        IE.Document.All(Var2).Value = ThisWorkbook.Sheets("Sheet1").Range("B6")
        IE.Document.All(Var3).Value = ThisWorkbook.Sheets("Sheet1").Range("B7")
    
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    
    
    Public Sub Data1 (ByRef Filename As String, ByRef Var1 As String, ByRef Var2 As String, ByRef Var3 As String)
    
        Filename = "FILE1.CSV"
        Var1 = "Response123"
        Var2 = "Response456"
        Var3 = "Response789"
    
    End Sub
    
    Public Sub Data2 (ByRef Filename As String, ByRef Var1 As String, ByRef Var2 As String, ByRef Var3 As String)
    
        Filename = "FILE2.CSV"
        Var1 = "Response987"
        Var2 = "Response654"
        Var3 = "Response321"
    
    End Sub
    
    'Public Sub Data3, Data4, etc...
    

2 个答案:

答案 0 :(得分:1)

如果在子程序/函数中声明(DIM)变量,则它仅在该子程序/函数中可用。

您需要公开声明变量,以供其他子程序使用。

或者,您可以重写代码,如下所示:

Private Sub Populate()

Dim strVar As String 
Dim IE As Object

DoEvents
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Navigate "https://website.com"
    .Visible = True
    While .busy
        DoEvents  
    Wend
End With
strVar=Variable1()

IE.Document.All(" & strVar & ").Value = ThisWorkbook.Sheets("Sheet1").Range("B5")

End Sub

Private Function Variable1() AS String
    Var = "R_575031"
End Sub

通过将例程转换为函数,您可以返回一个值。私有函数仅存在于模块中(例如,在表单中)。 Public Functions和subs属于全局模块容器,而不是Form / Sheet容器。

答案 1 :(得分:0)

这不是变量的工作方式,也不是参数如何工作。正如在注释中指出的那样,在过程中声明的变量只在这些过程中具有范围。如果您想从不同的程序更改您所在程序之外的值,您有2个选项 - 要么通过它们ByRef

Sub Variable1(ByRef Var As String)
    Var = "R_575031"
End Sub

'In the calling code...
Variable1 Var

...或将它们分配给函数的返回值:

Function Variable1() As String
    Variable1 = "R_575031"
End Function

'In the calling code...
Var = Variable1

在您的情况下,第二个使代码更清晰,更易于阅读。

至于使用,这一行是错误的:

E.Document.All(" & strVar & ").Value = ThisWorkbook.Sheets("Sheet1").Range("B5")

您不会将变量连接到命令中。如果需要传递参数,请传递参数:

E.Document.All(strVar).Value = ThisWorkbook.Sheets("Sheet1").Range("B5")
              '^^^^^^ this is a parameter of .All()