2017-10-17 75 views
1

我終於爲自己修復了Excel中的錯誤,其中有時看似隨機,數據透視表的源數據從「數據透視表32」變爲「樞軸表3」。數據透視表將數據源更改爲不同的數據透視表 - 微軟Excel錯誤

我一直手動編輯所有這些文本結束(這似乎從阻止它發生。)

我原來的腳本實際上揭示了另一個錯誤。 Excel更新了所有數據透視表名稱,但是如果腳本運行時它們不可見,圖表將完全失去源代碼。

所以,無論如何,這是我寫的VBA腳本。

Sub FixAllPivotTables() 

Dim pt As PivotTable 
Dim ws As Worksheet 

If MsgBox("This script is mostly harmless. It will add an 'x' to the end of every Pivot Table name to stop an Excel bug where sometimes Pivot Charts lose their connection to their original pivot. Pivot Charts that are visible work with just changing the name in code, but hidden ones lose their source data. So, this activates each sheet and then zooms to all. I assume it might break if your chart and your pivot table aren't on the same page? USE WITH CAUTION! Click 'Cancel' to quit gracefully without messing with anything.", vbOKCancel) = vbOK Then 

'change the settings 
For Each ws In ActiveWorkbook.Worksheets 
'Pivot Charts that are visible work with just changing the name in code, but hidden ones lose their source data. So, this activates each sheet and then zooms to all. I assume it might break if your chart and your pivot table aren't on the same page? USE WITH CAUTION! 
ws.Activate 
Cells.Select 
ActiveWindow.Zoom = True 
    For Each pt In ws.PivotTables 
    'This one changes the last character of the pivot name to append an "x" so that there are no issues 
    'with pivot charts losing their reference when there are >10 pivot tables. 
    If Right(pt.Name, 1) <> "x" Then 
     pt.Name = pt.Name + "x" 
     Debug.Print pt.Name + " I added an x" 
    Else 
     Debug.Print pt.Name + " had an x already" 
    End If 
    Next pt 
ActiveWindow.Zoom = 100 
Range("a1").Select 
Next ws 


MsgBox "Added an 'x' to the end of each pivot table name if it didn't have one already.", vbOKOnly 


Else 
MsgBox "Cancelled", vbOKOnly 
End If 

End Sub 

我知道有沒有錯誤捕獲,等等。但是,這是這些錯誤之一,當你使用一噸透視表和數據透視圖將發泄在最壞的時代浩劫沒有任何警告的。謝謝。 Jon

回答

0

將源數據數據透視表的標題更改爲不以2個數字結尾的內容似乎可以防止原始錯誤。只要所有透視圖與透視表位於同一頁面上,我上面提供的腳本就可以幫助您防止它。

Sub FixAllPivotTables() 

Dim pt As PivotTable 
Dim ws As Worksheet 

If MsgBox("This script is mostly harmless. It will add an 'x' to the end of every Pivot Table name to stop an Excel bug where sometimes Pivot Charts lose their connection to their original pivot. Pivot Charts that are visible work with just changing the name in code, but hidden ones lose their source data. So, this activates each sheet and then zooms to all. I assume it might break if your chart and your pivot table aren't on the same page? USE WITH CAUTION! Click 'Cancel' to quit gracefully without messing with anything.", vbOKCancel) = vbOK Then 

'change the settings 
For Each ws In ActiveWorkbook.Worksheets 
'Pivot Charts that are visible work with just changing the name in code, but hidden ones lose their source data. So, this activates each sheet and then zooms to all. I assume it might break if your chart and your pivot table aren't on the same page? USE WITH CAUTION! 
ws.Activate 
Cells.Select 
ActiveWindow.Zoom = True 
    For Each pt In ws.PivotTables 
    'This one changes the last character of the pivot name to append an "x" so that there are no issues 
    'with pivot charts losing their reference when there are >10 pivot tables. 
    If Right(pt.Name, 1) <> "x" Then 
     pt.Name = pt.Name + "x" 
     Debug.Print pt.Name + " I added an x" 
    Else 
     Debug.Print pt.Name + " had an x already" 
    End If 
    Next pt 
ActiveWindow.Zoom = 100 
Range("a1").Select 
Next ws 


MsgBox "Added an 'x' to the end of each pivot table name if it didn't have one already.", vbOKOnly 


Else 
MsgBox "Cancelled", vbOKOnly 
End If 

End Sub