来自NULL的VBA代码#VALUE! 归零

我正在使用下面的代码从网站检索一些数据。

Public Function giveMeValue(ByVal link As String) As String Set htm = CreateObject("htmlFile") With CreateObject("msxml2.xmlhttp") .Open "POST", link, False .send htm.body.innerhtml = .responsetext End With With htm.getelementbyid("JS_topStoreCount") giveMeValue = .innerText End With htm.Close Set htm = Nothing End Function 

有时候ID为"JS_topStoreCount"的元素不存在,函数返回#VALUE! 。 如何修改这个函数,使错误返回为0 ,并用红色突出显示?

我看不到Do Loop的原因,所以我删除了它,我添加了一个if语句来检查html元素是否分配给返回值。

 Public Function giveMeValue(ByVal link As String) As String Set htm = CreateObject("htmlFile") With CreateObject("msxml2.xmlhttp") .Open "GET", link, False .send htm.body.innerhtml = .responsetext End With If Not htm.getelementbyId("JS_topStoreCount") Is Nothing Then giveMeValue = htm.getelementbyId("JS_topStoreCount").innerText Else giveMeValue = "0" End If htm.Close Set htm = Nothing End Function