您可以刷新共享工作簿中的查询表。因此,如果它适合您的情况,您可以在模板中设置未共享的查询,然后共享它,然后通过 VBA 进行刷新。这是最简单的方法。
如果您无法预先设置,您可以使用 MSXML 获取 Web 数据并使用 VBA 将其粘贴到您的工作表中。您需要设置对 Microsoft XML、v5.0 和 Microsoft Forms 2.0 对象库(用于剪贴板)的引用(VBE - 工具 - 引用)。然后你可以运行这样的代码从网页中获取一个表格。
Sub GetData()
Dim oHttp As MSXML2.XMLHTTP50
Dim sHtml As String
Dim lTableStart As Long, lTableEnd As Long
Dim doClip As MSForms.DataObject
Const sTABLESTART As String = "<table id=""table1"">"
Const sTABLEEND As String = "</table>"
'create a new request object
Set oHttp = New MSXML2.XMLHTTP50
'open the request and send it
oHttp.Open "GET", "http://finance.yahoo.com/q?s=^GSPC", False
oHttp.send
'get the response - a bunch of html
sHtml = oHttp.responseText
'define where your data starts and ends
lTableStart = InStr(1, sHtml, sTABLESTART)
lTableEnd = InStr(lTableStart, sHtml, sTABLEEND)
'create a new clipboard object
Set doClip = New MSForms.DataObject
'set the text and put it in the clipboard
doClip.SetText Mid$(sHtml, lTableStart, lTableEnd - lTableStart)
doClip.PutInClipboard
'one of those rare instances where you actually have to select a range in VBA
Sheet4.Range("G10").Select
'blank out the previous results
Sheet4.Range("G10").CurrentRegion.ClearContents
'paste the hmtl as text with no formatting
Sheet4.PasteSpecial "Text", , , , , , True
End Sub
那里没有错误检查。您可能需要添加一些代码以确保您找到该网页并且它包含您想要的数据。