【发布时间】:2019-12-20 09:04:10
【问题描述】:
我已经复制了 Excel macro to search a website with excel data and extract specific results and then loop for next value 中的代码,尽管我在 URL_Get_SKU_Query1 = entityRange.Offset(0, 1).Value2 行出现错误,说明“未设置对象变量或块变量”
所以我只是想为另一个网站复制代码。 此代码拉入特定文本并从网站中吐出一个值。
所以我想在表 1 中输入 MFR SKU:
名称 // SKU // 价格 节水水龙头 // SS902BC
在我在工作表 2 上创建一个宏按钮并单击它之后
然后让它吐出价格。
所以结果如下:
名称 // SKU // 价格 节水水龙头 // SS902BC // 979.08
我需要这个才能在网站上查找多个项目。
Sub LoopThroughBusinesses1()
Dim i As Integer
Dim SKU As String
For i = 2 To Sheet1.UsedRange.Rows.Count
SKU = Sheet1.Cells(i, 2)
Sheet1.Cells(i, 3) = URL_Get_SKU_Query1(SKU)
Next i
End Sub
Function URL_Get_SKU_Query1(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;https://www.neobits.com/SearchBySKU.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 "Price"
Set entityRange = Sheet2.UsedRange.Find("Price")
' Then return the value of the cell to its' right
URL_Get_SKU_Query1 = entityRange.Offset(0, 1).Value2
' Clear Sheet2 for the next run
Sheet2.UsedRange.Delete
End Function
【问题讨论】:
-
也许为我们提供了一个完整的工作 url 和预期的结果?
-
请检查我对您帖子的编辑,看看它是否准确地代表了您想要的完整代码帖子。然后看看是否可以将代码量减少到演示问题所需的代码量。
-
您好,感谢您的输入,我已更改链接,以便将您定向到我最初找到代码的位置。我已经测试了链接中的代码,它工作正常,并提供了更多关于我希望代码做什么的信息。
-
你不能简单地假设你可以转移逻辑。您需要有效的网址。至少对我来说,构造 neobits.com/SearchBySKU.aspx?SearchText=SS902BC&safe=active 导致 404 页面未找到。除非我遗漏了某些东西(完全有可能),否则您的错误原因是无效的 url,这意味着您以后的选择器将无法找到。
-
从我使用 URL 的示例中;abr.business.gov.au/SearchByABN.aspx?SearchText= 工作正常,虽然我不确定 "SearchByABN.aspx?SearchText= 是什么意思,这种编码背后有什么原因吗?如果你愿意能够帮助我修改我的网址,使其正常工作,我将不胜感激。例如,当我进入 Excel 并执行宏时,我想从这个网址neobits.com/watersaver_faucet_ss902bc_ss902bc_p9597758.html 获得价格。