【发布时间】:2018-01-02 14:42:01
【问题描述】:
我正在尝试运行以下用户定义的函数,但收到以下错误:
对象变量或未设置块变量
Private Function Find_Select_Option(selectElement As HTMLSelectElement, optionText As String) As Integer
Dim i As Integer
Find_Select_Option = -1
i = 0
While i < selectElement.Options.length And Find_Select_Option = -1 ' ### error occurs on this line
DoEvents
If LCase(Trim(selectElement.Item(i).Text)) = LCase(Trim(optionText)) Then Find_Select_Option = i
i = i + 1
Wend
End Function
我在下面附上了 VBA 代码 (source)。请仔细检查一下,让我知道这段代码有什么问题。
Public Sub IE1()
Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
URL = "http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate URL
While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set HTMLdoc = .document
End With
'<select name="StreetDir">
Dim optionIndex As Integer
Dim dirSelect As HTMLSelectElement
Set dirSelect = HTMLdoc.getElementsByName("StreetDir")(0)
'dirSelect.selectedIndex = 2 'set option index directly
optionIndex = Find_Select_Option(dirSelect, "E")
If optionIndex >= 0 Then
dirSelect.selectedIndex = optionIndex
End If
'<select name="StreetSfx">
Dim suffixSelect As HTMLSelectElement
Set suffixSelect = HTMLdoc.getElementsByName("StreetSfx")(0)
optionIndex = Find_Select_Option(suffixSelect, "PLAZA")
If optionIndex >= 0 Then
suffixSelect.selectedIndex = optionIndex
End If
End Sub
我该如何解决这个问题?
【问题讨论】:
-
什么是 Excel 中的
HTMLSelectElement? -
您的 selectElement 变量
Is Nothing(在即时窗口中查看)。问题出在调用此函数的代码中。具体来说,实例化HTMLSelectElement的代码。 -
欢迎来到本站!查看tour 和how-to-ask page 了解更多关于提出可以吸引高质量答案的问题的信息。您可以edit your question 包含更多信息。我同意@DickKusleika。您能否显示调用
Find_Select_Option的代码?到目前为止,我看不出您发布的代码有任何明显的问题。 -
@Dick Kusleika 和 CXW,我也添加了代码。请检查并告诉我,这段代码有什么问题。提前致谢!!
-
感谢代码更新!顺便说一句,引用您的来源(和legally required 使用您从 Stack Overflow 获得的代码)很重要。我已将 OzGrid 源添加到您的问题中,以便为您提供示例。