我正在尝试测试从 Excel 数据连接运行的查询的三种变体。
我有三个单独的数据连接和三个单独的选项卡,分别从每个连接获取数据。
每个查询的连接字符串都是相同的,只是命令文本 (Oracle SQL) 不同。
Excel中有没有办法查看每个查询的执行时间?
我专门使用版本Excel 2016 MSO 16.0.4456.1003 64bit
也许是这样的(假设所有连接将其结果放在工作表中,而不是数据透视表中):
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
要运行这个:
-
Alt+F11 to go to the VBA editor.
- 从菜单:插入模块。
- 将代码粘贴到窗口中。
- 关闭 VBA 编辑器。
-
Alt+F8 brings up list of macro's. Pick the new one and click run.
-
Alt+F11 again to the VBA editor.
-
Ctrl+G opens the immediate pane with the results.
如果您希望代码写入单元格,请使用此版本:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)