2017-10-17 160 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