我有一个包含多个工作表的工作簿。 参数在一张纸上输入,我正在创建一个宏,它接受这些参数,将它们添加到连接字符串,然后更新每个工作表上的每个表。
我已经让它适用于一个连接,但我很难找到一种方法来存储每个连接名称,然后循环遍历每个连接添加变量。
这是我到目前为止的代码,其中一些是注释掉的...这就是我如何设法解决一个连接的问题。非常感谢任何指导。
Sub Update()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Parameters")
Dim startdate As Date
Dim enddate As Date
startdate = Range("week_start_date").Value
enddate = Range("week_end_date").Value
MsgBox "Values are " & startdate & " and " & enddate
Dim nm As ActiveWorkbook.Connections.name
Dim conn As WorkbookConnection
For Each conn In ActiveWorkbook.Connections
.CommandText = "exec dbo.'" & name & "', @start = '" & startdate & "', @end = '" & enddate & "' "
'With ActiveWorkbook.Connections("ps_STS_Op_Summary_Approvals").OLEDBConnection
'.CommandText = "exec dbo.ps_STS_Op_Summary_Approvals @start = '" & startdate & "', @end = '" & enddate & "'"
End With
'ActiveWorkbook.Connections("ps_STS_Op_Summary_Approvals").Refresh
For Each wSht In ThisWorkbook.Worksheets
For Each qt In wSht.QueryTables
qt.Refresh
Next
Next
End Sub
答案 0 :(得分:0)
查找存储在工作簿中的连接数,然后使用ActiveWorkbook.Connections的.item(x)属性循环遍历它们。
cnt = ActiveWorkbook.Connections.Count
For i = cnt To 1 Step -1
Set conn = ActiveWorkbook.Connections.Item(i)
'Do stuff to conn
Next i