如何优化 Excel VBA 查询选择器在脚本中选择修改后的 URL 的编码
How to optimize Excel VBA coding for queryselector selecting modified URL in script
我尝试了下面的代码。大部分时间 运行 卡在 on error goto 0
中并且在进一步的步骤 .fireevent ("onchange")
中没有成功。它可能是某种方式可以更好地优化流程。请帮助我,提前谢谢。
Public Sub makeselections()
Dim ie As New InternetExplorer, var As String, ele As Object
var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).value
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").value = var
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
On Error Resume Next
Set ele = .querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']")
On Error GoTo 0
Loop While ele Is Nothing
.querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']").Selected = True
.querySelector(".financials select").FireEvent "onchange"
End With
End With
End Sub
它可能被卡住了,因为 ele 仍然是 Nothing,即没有找到代码,或者至少没有找到 href 值。使用定时循环允许退出
Option Explicit
Public Sub MakeSelections()
Dim ie As New InternetExplorer, var As String, ele As Object, t As Date
Const MAX_WAIT_SEC As Long = 10
var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Value
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").Value = var
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
t = Timer
Do
On Error Resume Next
Set ele = .querySelector("[value$='quarter']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
ele.Selected = True
.querySelector(".financials select").FireEvent "onchange"
End With
End With
End Sub
我尝试了下面的代码。大部分时间 运行 卡在 on error goto 0
中并且在进一步的步骤 .fireevent ("onchange")
中没有成功。它可能是某种方式可以更好地优化流程。请帮助我,提前谢谢。
Public Sub makeselections()
Dim ie As New InternetExplorer, var As String, ele As Object
var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).value
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").value = var
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Do
On Error Resume Next
Set ele = .querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']")
On Error GoTo 0
Loop While ele Is Nothing
.querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']").Selected = True
.querySelector(".financials select").FireEvent "onchange"
End With
End With
End Sub
它可能被卡住了,因为 ele 仍然是 Nothing,即没有找到代码,或者至少没有找到 href 值。使用定时循环允许退出
Option Explicit
Public Sub MakeSelections()
Dim ie As New InternetExplorer, var As String, ele As Object, t As Date
Const MAX_WAIT_SEC As Long = 10
var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Value
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").Value = var
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
t = Timer
Do
On Error Resume Next
Set ele = .querySelector("[value$='quarter']")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If ele Is Nothing Then Exit Sub
ele.Selected = True
.querySelector(".financials select").FireEvent "onchange"
End With
End With
End Sub