vba - 错误 424 : Object Required Using Vlookup in a Loop

标签 vba excel excel-formula vlookup

基本上,我在这里想做的是让我的宏构建一个 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/

相关文章:

VBA 用换行符替换字符串

excel - 如何在 Excel 中的不同长度的文本之间提取用户 ID?

excel - 如何根据Excel中一列中的值合并行?

c# - ACE.OLEDB 提供程序错误地读取了一些列名称

c# - 使用 OpenXML 打开 Excel 工作表时文件包含损坏的数据错误

Excel 'put random value in a cell and run permutation "最简单的问题

excel - 将十进制MAC地址转换为十六进制MAC地址

javascript - Cors 请求在 Excel 中工作但不能在 Javascript 中工作

VBA Zip 文件错误

VBA 返回文本的 RGB 颜色