1
调用Sub过程
我的代码检索一个HTML页面为对象,给予一定的参数:Excel的VBA:从功能
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
但是,正如上面提到的,这将需要不同的功能相同的连接和对象。
我该如何解决这个问题?由于
这两种方法使用相同数量的资源。我没有看到差异。 – jacouh
你有没有把'oHtml'用作'公共变量'? –
@KazJaw Bingo,就是这么简单! –