错误424:在循环中使用Vlookup所需的对象

基本上我在这里试图做的是让我的macrosbuild立一个URL将被刮,并用于填充信息。 我打算根据指定的具体证券和数据领域的URL是唯一的。

我在我的j循环中收到一个错误,在一个vlookup中使用数据typesstring。 我打算为它返回一个值将被用来填充我的url。

Dim Last1 As Integer: Last1 = W.Range("A1000").End(xlUp).Row Dim Last2 As Integer: Last2 = W.Range("XFD1").End(xlToLeft).Column Dim IE As SHDocVw.InternetExplorer Dim html As HTMLDocument If Last = 1 Then Exit Sub Dim Symbols As String Dim DataType As String Dim URLParameters As String Dim i, j As Integer For i = 2 To Last1 Symbols = Symbols & Worksheets("Stock Prices").Range("A" & i).Value & "+" Next i Symbols = Left(Symbols, Len(Symbols) - 1) Debug.Print Symbols For j = 2 To Last2 DataType = DataType & Worksheets("Stock Prices").Cells(1, j).Value URLParameters = Application.WorksheetFunction.VLookup(DataType, Worksheets("URL Info").Range("URL_DataInfo"), 2).Value Next j Debug.Print DataType Debug.Print URLParameters Set IE = CreateObject("InternetExplorer.Application") 'Tells IE where to pull IE.navigate "https://download.finance.yahoo.com/d/quotes?s=" & Symbols & "&f=" & URLParameters IE.Visible = True 'Wait until IE is done loading page Do While IE.readyState <> READYSTATE_COMPLETE Application.StatusBar = "Pulling data..." DoEvents Loop 'show text of HTML document returned Set html = IE.document 'MsgBox html.DocumentElement.innerHTML 'close down IE and reset status bar Set IE = Nothing Application.StatusBar = "" 'Remove HTML tags Dim info As String info = cook_tags(CStr(html.DocumentElement.innerHTML)) 'Split the results into cells Call split_data(info) End Sub 

您当前的代码要求VLOOKUP函数进行适当的匹配。 数据必须按升序sorting,以便远程工作。

使用变体types来接受返回的值并删除WorksheetFunction对象 。 如果单独使用Excel应用程序对象 ,则可以将错误返回到变体。

你不返回一个Range.Value属性 ,只是一个VLOOKUP的返回值。

 Dim URLParameters As variant URLParameters = Application.VLookup(DataType, Worksheets("URL Info").Range("URL_DataInfo"), 2, FALSE) if IsError(URLParameters) Then 'no value found else 'value found and put in URLParameters end if 

VLOOKUP公式返回匹配单元格的 。 尝试

 URLParameters = Application.WorksheetFunction.VLookup(DataType, Worksheets("URL Info").Range("URL_DataInfo"), 2) 

取而代之(注意函数调用结束时的.Value ,这是不正确的):

 URLParameters = Application.WorksheetFunction.VLookup(DataType, Worksheets("URL Info").Range("URL_DataInfo"), 2).Value 

如果Vlookup函数的返回数据types不是string(即,尤其是错误types),则可能会收到更多错误。 在这种情况下,最好testing错误,并避免使用WorksheetFunction类:

 With Worksheets("URL Parameters") If Not IsError(Application.VLookup(DataType, .Range("URL_DataInfo"), 2)) Then URLParameters = Application.VLookup(DataType, .Range("URL_DataInfo"), 2) Else '## Here, you'll need some error handling or exit sub early, etc. End If End With