[英]Excel VBA: Call Sub Procedure from function
给定某些参数,我的代码将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
我正在定义将使用同一对象的函数,因此,我想减少连接数。 所以我想定义一个像这样的函数:
Function myFunction(myparam As String)
Call MyPage(myparam)
'code here
End Function
但这是行不通的。 当我在单元格中键入= myFunction时,我得到了#VALUE! 错误。
如果我只是在函数内键入子过程的代码,它就可以工作,类似于:
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
但是,如上所述,对于不同的功能,这将需要相同的连接和对象。
我该如何解决? 谢谢
将我的评论转换为答案:
将您的oHtml variable
用作Public variable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.