you can use a variation of this (it's for changing the commandtext of a pivot)
Sub ChangePivot()
Dim pc As PivotCache
Set pc = ActiveSheet.PivotTables(1).PivotCache
ChangePC_SQL pc, "SELECT `Sheet1$`.F1, `Sheet1$`.F2, `Sheet1$`.F3, `Sheet1$`.F4 FROM `C:\Book1`.`Sheet1$` `Sheet1$` WHERE `Sheet1$`.F1 = 2"
End Sub
Sub ChangePC_SQL(pc As PivotCache, strSQL As String)
Dim blnODBC As Boolean
With pc
If .QueryType = xlODBCQuery Then
blnODBC = True
.Connection = Replace$(.Connection, "ODBC;DSN", "OLEDB;DSN", 1, 1, vbTextCompare)
End If
If StrComp(.CommandText, strSQL, vbTextCompare) <> 0 Then
.CommandText = strSQL
End If
If blnODBC = True Then
.Connection = Replace(.Connection, "OLEDB;DSN", "ODBC;DSN", 1, 1, vbTextCompare)
End If
.Refresh
End With
End Sub
which will change the connection to OLEDB at the same time you change the commandtext (you can just leave the commandtext the same)
you can refer to pivot tables by name as well as index, or just loop through each one on each sheet using a for each... next loop
Bookmarks