2010-04-25 1 views
0

OLAP 유형 분석을 위해 .cub 파일을 기반으로하는 피벗 테이블과 함께 Excel을 사용하는 경우가 있습니다. 이것은 xls를 옮기고 자 할 때를 제외하고는 훌륭합니다. 내부적으로는 .cub 파일의 위치에 대한 비 상대적 참조가 있습니다. .cub 파일에 의존하는 xls 파일을 편리하게 이동할 수있게하려면 어떻게 대처할 수 있습니까?피벗 테이블에 사용되는 .cub 파일의 위치를 ​​변경하는 Excel 매크로? (.cub 파일에 의존하는 .xls 파일을 이동할 수 있도록)

가장 좋은 대답은 피벗 테이블의 .cub 파일 위치에 대한 참조를 업데이트하는 매크로를 작성하는 것입니다.

답변

0

다음은 매크로입니다. 분명히 이것은 당신에게 맞지 않을 수도있는 몇 가지 가정을 만듭니다. 통합 문서의 모든 피벗 테이블을 업데이트하여 동일한 .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 
관련 문제