[英]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.