问题描述
我正在尝试测试从Excel数据连接运行的查询的三种变体.
I am trying to test out three variations of a query that is ran from an Excel data connection.
我有三个单独的数据连接和三个单独的选项卡,分别从每个连接获取数据.
I have three individual data connections and three individual tabs that get the data from each connection respectively.
每个查询的连接字符串相同,只有命令文本(Oracle SQL)不同.
The connection string is identical for each query, only the command text (Oracle SQL) is different.
我专门使用版本Excel 2016 MSO 16.0.4456.1003 64bit
推荐答案
可能是这样的(假设所有连接将其结果放置在工作表中,而不是在数据透视表中):
Something like this perhaps (assumes all connections place their results in a worksheet table, not in a pivottable):
Sub TimeQueries()
Dim oSh As Worksheet
Dim oCn As WorkbookConnection
Dim dTime As Double
For Each oCn In ThisWorkbook.Connections
dTime = Timer
oCn.Ranges(1).ListObject.QueryTable.Refresh False
Debug.Print Timer - dTime, oCn.Name, oCn.Ranges(1).Address(external:=True)
Next
End Sub
要运行此命令:
- + 进入VBA编辑器.
- 从菜单:插入模块.
- 在窗口中粘贴代码.
- 关闭VBA编辑器.
- + 会显示宏列表.选择新的,然后单击运行. 再次将
- + 转到VBA编辑器.
- l + 打开立即窗格,显示结果.
- + to go to the VBA editor.
- From menu: Insert Module.
- Paste code in the window.
- Close VBA editor.
- + brings up list of macro's. Pick the new one and click run.
- + again to the VBA editor.
- l+ opens the immediate pane with the results.
如果您想将代码写入单元格,请使用以下版本:
If you want the code to write to a cell, use this version:
Sub TimeQueries()
Dim oSh As Worksheet
Dim oCn As WorkbookConnection
Dim dTime As Double
Dim lRow As Long
Set oSh = Worksheets("Sheet4") 'Change to your sheet name!
oSh.Cells(1,1).Value = "Name of Connection"
oSh.Cells(1,2).Value = "Location"
oSh.Cells(1,1).Value = "Refresh time (s)"
For Each oCn In ThisWorkbook.Connections
lRow = lRow + 1
dTime = Timer
oCn.Ranges(1).ListObject.QueryTable.Refresh False
oSh.Cells(lRow,3).Value = Timer - dTime
oSh.Cells(lRow,1).Value = oCn.Name
oSh.Cells(lRow,2).Value = oCn.Ranges(1).Address(external:=True)
Next
End Sub
这篇关于Excel ODBC数据连接查询刷新每个查询所花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!