Option Explicit
Private Sub listCaches()
Dim selectedCache As PivotCache
For Each selectedCache In ThisWorkbook.PivotCaches
Debug.Print selectedCache.Index
Debug.Print selectedCache.Connection
Next selectedCache
End Sub
您可以访问要编辑的连接:
ThisWorkbook.PivotCaches(yourIndex).Connection
注意:更改连接后,您应呼叫:
ThisWorkbook.PivotCaches(yourIndex).Refresh
编辑:
您可以更改CommandText,而不是更改SourceData。这应该有同样的效果。以下代码适用于我:
ThisWorkbook.PivotCaches(1).CommandText = "SELECT movies.title, movies.rating, movies.comments FROM `C:\Folder\moviesDB`.movies movies"
ThisWorkbook.PivotCaches(1).Refresh
这段代码还更新了我的SourceData。
编辑2:
Sheets("mySheet").PivotTables("PivotTable1").PivotCache.CommandText = "SELECT movies.title as meh, movies.rating, movies.comments FROM `C:\Folder\moviesDB`.movies movies"
Sheets("mySheet").PivotTables("PivotTable1").PivotCache.Refresh
注意:moviesDB是一个.mdb文件,movies是表/查询
注2:这也可能有助于您
Debug.Print
更改工作命令之前,请先删除该文本。这将为您的新CommandText提供一个模板。