【问题标题】:Error 91 VBscript parsing html text to excel错误 91 VBscript 将 html 文本解析为 excel
【发布时间】:2021-08-30 18:48:41
【问题描述】:

我试图从这个论坛模拟一个 VB 脚本: Search a website with Excel data to extract results and then loop

我在这一行遇到错误:

URL_Get_ABN_Query = entityRange.Offset(0, 1).Value2

错误是: 错误 91 对象变量或未设置块变量

这是该论坛的两部分脚本:

Sub LoopThroughBusinesses()
    Dim i As Integer
    Dim ABN As String
    For i = 2 To Sheet1.UsedRange.Rows.Count
        ABN = Sheet1.Cells(i, 2)
        Sheet1.Cells(i, 3) = URL_Get_ABN_Query(ABN)
    Next i
End Sub

Function URL_Get_ABN_Query(strSearch As String) As String   ' Change it from a Sub to a Function that returns the desired string
' strSearch = Range("a1") ' This is now passed as a parameter into the Function
Dim entityRange As Range
With Sheet2.QueryTables.Add( _
        Connection:="URL;http://www.abr.business.gov.au/SearchByABN.aspx?SearchText=" & strSearch & "&safe=active", _
        Destination:=Sheet2.Range("A1"))   ' Change this destination to Sheet2

    .BackgroundQuery = True
    .TablesOnlyFromHTML = True
    .Refresh BackgroundQuery:=False
    .SaveData = True
End With

' Find the Range that has "Finish"
Set entityRange = Sheet2.UsedRange.Find("Entity type:")

' Then return the value of the cell to its' right
URL_Get_ABN_Query = entityRange.Offset(0, 1).Value2

' Clear Sheet2 for the next run
Sheet2.UsedRange.Delete

End Function

【问题讨论】:

  • 看起来你的 Find() 没有匹配。
  • 使用find() 时,您应该始终测试是否有匹配项——在这种情况下为If Not entityRange Is Nothing Then
  • 嗨蒂姆,感谢您的评论。当我调试它总是突出显示这部分
  • 嗨蒂姆,感谢您的评论。当我调试时,它总是突出显示这部分(第 29 行): URL_Get_ABN_Query = entityRange.Offset(0, 1).Value2
  • 更新您的问题并在此处添加您的代码。 cmets 中的代码不易阅读。

标签: excel vba web-scraping html-parsing


【解决方案1】:

根据上面的cmets:

Sub Tester()

    Debug.Print URL_Get_ABN_Query("44627939854") '>> Discretionary Trading Trust
    Debug.Print URL_Get_ABN_Query("XXXX")        '>> Not found
    
End Sub

Function URL_Get_ABN_Query(strSearch As String) As String
    
    Dim entityRange As Range
    With Sheet2.QueryTables.Add( _
        Connection:="URL;http://www.abr.business.gov.au/SearchByABN.aspx?SearchText=" & strSearch & "&safe=active", _
        Destination:=Sheet2.Range("A1"))

        .BackgroundQuery = True
        .TablesOnlyFromHTML = True
        .Refresh BackgroundQuery:=False
        .SaveData = True
    End With

    'when using Find, you should always specify at least the `lookat` argument...
    '  xlPart (matches part) or xlWhole (full match)
    Set entityRange = Sheet2.UsedRange.Find(what:="Entity type:", lookat:=xlPart)
    
    'check to see if a match was made by Find()
    If Not entityRange Is Nothing Then
        URL_Get_ABN_Query = entityRange.Offset(0, 1).Value2
    Else
        URL_Get_ABN_Query = "Not found"
    End If
    Sheet2.UsedRange.Delete ' Clear Sheet2 for the next run

End Function

【讨论】:

  • 即使我用我的原件替换了你的前半部分,我仍然得到空白结果。
  • 发布的代码对我来说效果很好 - 您是否按原样尝试过?
  • 是的。你得到了什么结果?我在 Sheet2 中得到空白或 null
  • 我得到了立即窗口中显示的文本(如果没有为您打开,请在 VB 编辑器中使用 Ctrl+G)。表 2 中没有任何内容,因为 URL_Get_ABN_Query 的最后一行是 Sheet2.UsedRange.Delete
  • 结果如下: 未找到全权委托交易信托 未找到全权委托交易信托。我需要以某种方式自动化我的解析。我喜欢将超过一千行的结果放在 Sheet2 中。我的真正目标是从hardwareresources.com/307fu.html?item_overall_length= 解析饰面类型,示例项目编号如下:209FU20 209FU22 209FU24 例如,如果您尝试:hardwareresources.com/307fu.html?item_overall_length=209FU20 您将了解到这是一种清晰的锌饰面,感谢您的帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多