调用子之后传递variables

我有两个问题。

  1. 因为Var3有时会根据Sub而变为空白,所以我得到一个424错误,单词undefined被复制到网站上的文本框中 – 但是我需要把它留空,而不是在这种情况下粘贴任何东西。

  2. 我在Excel中有一个下拉框来selectSub将被调用。 我想将它融入到IF语句中,如下所示,但是当我尝试代码时,什么都不会发生。

这是我的尝试:

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... 

这不是如何variables的工作,而不是如何参数的工作。 正如在评论中指出的那样,在程序中声明的variables只在这些程序中有范围。 如果你想在不同的程序中改变你所使用的程序的值,你有两个select – 要么传递它们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") 

您不要将variables连接到命令中。 如果你需要传递一个参数,传递一个参数:

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

如果在子程序/function中声明(DIM)variables,则只能在该子程序/function中使用。

您需要公开声明这些variables以供其他子程序使用。

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

 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 

通过将例程转换为函数,您可以返回一个值。 私有函数只存在于模块中(例如在一个表单中)。 公共函数和潜在属于全局模块容器而不是表单/工作表容器。