[英]Excel VBA: Call Sub Procedure from function
My code is retrieving an HTML page as an object, given a certain parameter: 给定某些参数,我的代码将HTML页面作为对象检索:
Public Sub MyPage(myparam)
Dim oHtml As HTMLDocument
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.example.com" & myparam, False
.send
oHtml.body.innerHTML = .responseText
End With
End Sub
I'm defining functions that will use the same object, therefore, I want to minimize the number of connections. 我正在定义将使用同一对象的函数,因此,我想减少连接数。 So I want to define a function like: 所以我想定义一个像这样的函数:
Function myFunction(myparam As String)
Call MyPage(myparam)
'code here
End Function
But this isn't working. 但这是行不通的。 When I type =myFunction into a cell, I get the #VALUE! 当我在单元格中键入= myFunction时,我得到了#VALUE! error. 错误。
If I just type the code of the sub procedure inside the function, it works, something like: 如果我只是在函数内键入子过程的代码,它就可以工作,类似于:
Function myFunction(myparam As String)
Dim oHtml As HTMLDocument
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.example.com" & myparam, False
.send
oHtml.body.innerHTML = .responseText
End With
'code here
End Function
But, as mentioned above, this will require the same connection and object for different functions. 但是,如上所述,对于不同的功能,这将需要相同的连接和对象。
How can I solve this? 我该如何解决? Thanks 谢谢
Converting my comment to an answer: 将我的评论转换为答案:
Use your oHtml variable
as Public variable
. 将您的oHtml variable
用作Public variable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.