我有一个宏,它可以打开网页,分配某些变量,然后使用Call
函数调用另一个过程。我将需要在第二个过程中使用第一个过程中定义的某些变量(例如numberOfPages
),但是会出现以下错误:
编译错误:尚未定义变量
有人可以告诉我错误是什么以及如何解决吗?我将在下面两个步骤中详细说明。
Sub test()
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim ie As InternetExplorer
Dim numberOfPages As Double
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://cebra.com.ar/category/73/Juego-de-Construccion.html"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading Web page …"
DoEvents
Loop
Set html = ie.document
Set elements = html.getElementsByClassName("container")
Set ElementCol = html.getElementsByTagName("a")
numberOfPages = ie.document.querySelectorAll(".setPage").Length
'MsgBox numberOfPages
For Each ele In ie.document.getElementsByTagName("li")
For Each element In elements
If element.className = "container" Then
'Do something
Call procedure
End If
Next element
Next
MsgBox "Done"
End Sub
Option Explicit
Public Sub procedure()
MsgBox numberOfPages
'I want to use the internet explorer opened and this variable to do something
End Sub
答案 0 :(得分:1)
正如@Karlomanio所说,您需要将变量传递给子过程(请不要将其称为“过程”)。
这是您的代码,已调整为使numberOfPages
起作用:
Option Explicit
Public Sub WebProcedure(numberOfPages as Long)
MsgBox numberOfPages
'Do something here based on the number of pages
End Sub
这将在您的test()
子句中使用以下行来调用:
WebProcedure numberOfPages