다음은 매크로입니다. 분명히 이것은 당신에게 맞지 않을 수도있는 몇 가지 가정을 만듭니다. 통합 문서의 모든 피벗 테이블을 업데이트하여 동일한 .cub 파일을 사용합니다.
워크 북의 피벗 테이블 연결을 반복하여 .xls 파일과 동일한 이름의 .cub 파일을 동일한 디렉토리에 사용합니다. 이 경우 PivotCaches가 LocalConnections를 사용하지 않는다고 가정합니다. ActiveWorkbook.PivotCaches (1) .UseLocalConnection = False인지 확인합니다.
Sub UpdatePivotTableConnections()
Dim sNewCubeFile As String
sNewCubeFile = ActiveWorkbook.Path & Replace(ActiveWorkbook.Name, ".xls", ".cub", , , vbTextCompare)
Dim iPivotCount As Integer
Dim i As Integer
iPivotCount = ActiveWorkbook.PivotCaches.Count
' Loop through all the pivot caches in this workbook. Use some
' nasty string manipulation to update the connection.
For i = 1 To iPivotCount
With ActiveWorkbook.PivotCaches(i)
' Determine which cub file the PivotCache is currently using
Dim sCurrentCubeFile As String
Dim iDataSourceStartPos As Integer
Dim iDataSourceEndPos As Integer
iDataSourceStartPos = InStr(1, .Connection, ";Data Source=", vbTextCompare)
If iDataSourceStartPos > 0 Then
iDataSourceStartPos = iDataSourceStartPos + Len(";Data Source=")
iDataSourceEndPos = InStr(iDataSourceStartPos, .Connection, ";", vbTextCompare)
sCurrentCubeFile = Mid(.Connection, iDataSourceStartPos, iDataSourceEndPos - iDataSourceStartPos)
' If the PivotCache is using a different cub file then update the connection to use the new one.
If sCurrentCubeFile <> sNewCubeFile Then
.Connection = Left(.Connection, iDataSourceStartPos - 1) & sNewCubeFile & Right(.Connection, Len(.Connection) - iDataSourceEndPos + 1)
End If
End If
End With
Next i
End Sub