【发布时间】: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