所以我是vba的新手,我试图获得价格。宏是:
Sub GetPrice()
Dim objIE As InternetExplorer
Dim Prc1 As String
Set objIE = New InternetExplorer
Dim Search_Terms() As Variant
Dim CopiedData() As Variant
Dim y As Integer
objIE.Visible = False
Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A50").Value)
ReDim CopiedData(LBound(Search_Terms) To UBound(Search_Terms))
y = 2
For a = LBound(Search_Terms) To UBound(Search_Terms)
objIE.navigate "https://Example.com/?loc=shop_search&app=578080_2&sort=lh&search_item=" & Search_Terms(a)
Do: DoEvents: Loop Until objIE.readyState = 4
Prc1 = objIE.document.getElementsByClassName("item-amount")(0).innerText
Sheets("Sheet1").Range("B" & y).Value = Prc1
objIE.navigate "http://Example2.com/market/search?appid=578080&q=" & Search_Terms(a)
Do: DoEvents: Loop Until objIE.readyState = 4
Prc1 = objIE.document.getElementsByClassName("market_table_value")(1).getElementsByTagName("span")(0).innerText
Sheets("Sheet1").Range("D" & y).Value = Prc1
Dim i As String
Dim k As String
i = "TL"
k = ""
Columns("D").Replace what:=i, replacement:=k, lookat:=xlPart, MatchCase:=False
y = y + 1
Next
ActiveSheet.Range(Cells(1, 2), Cells(UBound(CopiedData), 2)) = Application.Transpose(CopiedData)
objIE.Quit
End Sub
当我尝试机会时:
Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A121").Value)
至:
Search_Terms = Application.Transpose(ActiveSheet.Range("A2:A50").Value)
它进入A50并删除所有结果excel并停止。我该怎么办 ?你可以解释和写出正确的代码吗?
答案 0 :(得分:1)
此代码行:
ActiveSheet.Range(Cells(1, 2), Cells(UBound(CopiedData), 2)) = Application.Transpose(CopiedData)
...最初设计用于将CopiedData中的值放回工作表中。但是,您不再填充CopiedData;您正在使用Prc1将值写回工作表。
删除或“评论”'上面的代码行。