我正在尝试使用VBA从网站上获取37.59%: -
<span id="dgCompShare_Label23_5">37.59%</span>
我已设法导航到该网站并进行各种选择。以下是我的相关VBA代码。
Dim appIE As Object
Dim f As Object
Dim ws As Worksheet
Dim wb As Workbook
Set wb = Application.Workbooks("Macro2")
Set ws = wb.Worksheets("Sheet1")
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://website.com/"
.Visible = True
Do While appIE.Busy
DoEvents
Loop
Set f = appIE.document.getElementbyid("dgCompShare_Label23_5")
If Not f Is Nothing Then percent = f.innerText
With ws
.Cells(4, 3) = percent
End With
我收到运行时错误&#39; 424&#39 ;:对象需要: 设置f = appIE.document.getElementbyid(&#34; dgCompShare_Label23_5&#34;) 我不明白为什么会这样。
答案 0 :(得分:4)
当页面未完全加载或在内部加载并且您尝试设置对象时会发生这种情况。
如果您确定html页面上提供了您要查找的元素,请尝试一下,看看这是否适合您。
On Error Resume Next
Do While appIE.Busy And f Is Nothing
DoEvents
Set f = appIE.document.getElementbyid("dgCompShare_Label23_5")
Loop
Percent = f.innerText
或者还要检查ieApp对象的ReadyState,然后设置对象变量...
Do While appIE.busy Or appid.readystate <> 4
DoEvents
Loop
Set f = appIE.document.getElementbyid("dgCompShare_Label23_5")