excel - Error handling when 'getElementById' can't find object/value
问题描述
I have an excel file that contains ID numbers in column C starting at the second row.
There's about 4 cells that need to be skipped because they have a different value (hence the len > 8 if statement).
The script loops through the columns. Uses the ID to concatenate a hyperlink, that the script then uses to read out a specific piece of data on that website. The 11th character of that string is compared to the cell value to the right of the active cell. If it's the same, the cell is made white. If it's different, the cell is highlighted in yellow. Basically this script will alert the excel user if the data is different from the website.
My script works fine when if can find the data. However, in about 30% of cases, the value that my 'get elementbyid' statement searches for does not exist. I need to alter my script to skip through these cases and can not get it right.
Current script:
Sub specs()
Range("C2").Activate
Do While ActiveCell.Value <> ""
If Len(ActiveCell.Value) > 8 Then
Dim YouthID As String
YouthID = ActiveCell.Value
Dim URLstart As String
URLstart = "https://www.hattrick.org/en/Club/Players/YouthPlayer.aspx?YouthPlayerID="
Dim URL As String
URL = URLstart & YouthID
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate URL
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set allrowofdata = appIE.document.getElementById("ctl00_ctl00_CPContent_CPMain_trSpeciality")
Dim myValue As String: myValue = allrowofdata.Cells(1).innerHTML
Dim result As String
result = Mid(myValue, 11, 1)
appIE.Quit
Set appIE = Nothing
If ActiveCell.Offset(0, 1).Value <> result Then
ActiveCell.Offset(0, 1).Interior.ColorIndex = 6
Else
ActiveCell.Offset(0, 1).Interior.ColorIndex = 0
End If
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
When this script encounters a web page that doesn't contain the value I get a 'type mismatch' error on the getelementbyid line.
Searching through stackkoverflow I found 3 possible solutions:
- If
IsObject(allrowofdata)
then => doesn't work for me, I get the same type mismatch error before the code reaches that line. (right behind getelementbyid)
Set allrowofdata = appIE.document.getElementById("ctl00_ctl00_CPContent_CPMain_trSpeciality")
If IsObject(allrowofdata) Then
Dim myValue As String: myValue = allrowofdata.Cells(1).innerHTML
Dim result As String
result = Mid(myValue, 11, 1)
Else
result = ""
End If
- if not appIE.documnent [...] is nothing then => object required error
If Not appIE.document.getelementbyid("ctl00_ctl00_CPContent_CPMain_trSpeciality") Is Nothing Then
Set allrowofdata = appIE.document.getelementbyid("ctl00_ctl00_CPContent_CPMain_trSpeciality")
Dim myValue As String: myValue = allrowofdata.Cells(1).innerHTML
Dim result As String
result = Mid(myValue, 11, 1)
Else
result = ""
End If
- with error resume next I get through it, but it highlights the cell every time there is no data because my result string is "" and my activecell.offset value is 'empty', so they are defined as being different => highlight.
Dim result As String
result = ""
On Error Resume Next
Set allrowofdata = appIE.document.getelementbyid("ctl00_ctl00_CPContent_CPMain_trSpeciality")
On Error Resume Next
Dim myValue As String: myValue = allrowofdata.Cells(1).innerHTML
On Error Resume Next
result = Mid(myValue, 11, 1)
Can someone tell me what the final fix is? And preferably also what's wrong in the first two scenario's.
解决方案
既然你知道allrowofdata
可能什么都不是,你需要提防这种情况。因此,在 Set 语句之后立即添加一个 If 块:
On Error Resume Next
Set allrowofdata = appIE.document.getElementById("ctl00_ctl00_CPContent_CPMain_trSpeciality")
On Error GoTo 0
If Not allrowofdata Is Nothing then
Dim myValue As String: myValue = allrowofdata.Cells(1).innerHTML
Dim result As String
result = Mid(myValue, 11, 1)
appIE.Quit
Set appIE = Nothing
If ActiveCell.Offset(0, 1).Value <> result Then
ActiveCell.Offset(0, 1).Interior.ColorIndex = 6
Else
ActiveCell.Offset(0, 1).Interior.ColorIndex = 0
End If
End If
推荐阅读
- python - Python 中的 if 与 Elif,哪个更好?
- microservices - 我可以在单体或微服务中同时拥有 fontend(pages) 和后端吗?
- oracle - NLS_TIMESTAMP_FORMAT 在客户端设置在哪里?
- swift - 按钮鼠标向上事件期间的鼠标移动事件
- c - 计算 3 个动态表的中位数、平均值和标准差并将它们添加到文件中
- vba - 查找/替换 VBA 代码未正确执行
- ios - React Native v0.62 升级增加 iOS .ipa 大小
- jquery - 自动完成焦点显示默认下拉似乎不起作用
- android - 在 Kotlin 中使用 @Parcelize 注释时如何忽略字段
- sql-server - 从 SQL Server 或 powerBuilder 中的主详细信息行创建 XML