基本上,我在这里想做的是让我的宏构建一个 URL,该 URL 将被抓取并用于填充信息。我打算根据指定的特定证券和数据字段使 URL 保持唯一。
我在 vlookup 中使用数据类型字符串时在 j 循环中收到错误。我打算让它返回一个用于填充我的 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 function 进行适当的匹配。数据必须按升序排序才能远程工作。
使用变体类型接受返回值并摆脱 WorksheetFunction object 。如果单独使用 Excel Application object,则可以将错误返回给变体。
您没有返回 Range.Value property ,而只是返回 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
关于vba - 错误 424 : Object Required Using Vlookup in a Loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39711628/