html - VBA On Error GoTo 不工作
问题描述
我正在查看一个特定的 div 标签,如果没有 innerText,我想跳过它并转到 NextLink3。我认为当没有 innerText 但是,我只是得到一个运行时错误: subscript out of range at test = arr(LBound(arr))
,我不知道如何处理。我认为“On Error GoTo”会查看下一行是否产生错误,如果确实如此,请转到其他地方,但我的以下代码没有这样做,我仍然得到下标错误。
HTML 代码:
<div style="width: 555px; -ms-overflow-x: auto; -ms-overflow-y: hidden;">
<a href="/kegg-bin/ddi_list?drug=D00550">
<img name="DDI" align="middle" onmouseover="btn(this,'DDIbh')" onmouseout="btn(this,'DDIb')" onmousedown="btn(this,'DDIbd')" onmouseup="btn(this,'DDIb')" ontouchstart="btn(this,'DDIbd')" ontouchend="btn(this,'DDIb')" alt="Drug interaction" src="/Fig/bget/button_DDIb.gif" border="0">
</a>
</div>
我的 VBA 代码:
Dim ele As Object, test As String
Set ele = html.querySelectorAll(".td50 div")(3)
If Not ele Is Nothing Then
On Error GoTo NextLink3
arr = Split(ele.innerText, Chr$(10))
On Error GoTo NextLink3
test = arr(LBound(arr))
If InStr(arr(LBound(arr)), "[HSA") = 0 Or InStr(arr(LBound(arr)), " [KO") = 0 Then GoTo NextLink3
For i = LBound(arr) To UBound(arr)
Debug.Print Split(arr(i), "[")(0)
Next i
GoTo NextLink
End If
NextLink3:
...
解决方案
Len()
您可以在尝试使用该函数 拆分它之前检查以确保您的 html 元素具有值。
我不能确定哪些其他代码不再需要,例如NextLink3
仅用于处理我们现在避免的错误。
Dim ele As Object, test As String
Set ele = HTML.querySelectorAll(".td50 div")(3)
If Not ele Is Nothing Then
If Len(ele.innerText) > 0 Then
On Error GoTo NextLink3
arr = Split(ele.innerText, Chr$(10))
On Error GoTo NextLink3 'probably not necessary unless you call ... Goto 0 in NextLink3
test = arr(LBound(arr))
If InStr(arr(LBound(arr)), "[HSA") = 0 Or InStr(arr(LBound(arr)), " [KO") = 0 Then GoTo NextLink3
For i = LBound(arr) To UBound(arr)
Debug.Print Split(arr(i), "[")(0)
Next i
GoTo NextLink
End If
End If
推荐阅读
- hibernate - Hibernate 选择不正确的 TypeDescriptor
- javascript - OpenLayers - 更改视图时地图消失了
- ivy - Ivy 将 pom.xml-Package-Tag 转换为 ivy.xml-Extension
- android - 无法将浮动操作按钮设置为剪切导航
- rabbitmq - 最后一条消息后的兔子 MQ 延迟
- json - Powershell 将 json 字符串转换为 xml 字符串总是产生 System.String
- c# - “对象不能从 DBNull 转换为其他类型。”
- c# - 如何使用 MVVM 从另一个 WPF 打开 WPF 应用程序?
- php - 如何在通过 VPN 连接的任何计算机上通过浏览器访问 http://localhost/project?
- html - 滚动条未显示在我的第一个网站文件中