简体   繁体   English

数据透视图将源更改为其他数据透视表-Microsft Excel错误

[英]Pivot chart changes source to different Pivot Table - Microsft Excel Bug

I finally made myself a fix for the bug in Excel where sometimes, seemingly at random, the source data for a Pivot Chart changes from, say "Pivot Table 32" to "Pivot Table 3". 我终于为自己修复了Excel中的错误,有时,数据透视图的源数据有时从“数据透视表32”变为“数据透视表3”,这似乎是随机的。

I had been hand editing all of these to end in text (which seems to stop it from happening.) 我一直在手工编辑所有这些以文本结尾(这似乎阻止了它的发生)。

My original script actually revealed another bug. 我的原始脚本实际上揭示了另一个错误。 Excel updated all of the pivot table names, but then the charts would completely lose their source if they weren't visible when the script ran. Excel更新了所有数据透视表名称,但是如果脚本运行时不可见,则图表将完全丢失其源。

So, anyway, here's the VBA script I wrote. 因此,无论如何,这是我编写的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

I know there's not error trapping, etc. But, this is one of those bugs when you're using a ton of pivot table and pivot charts that will wreak havoc at the worst times without any warning. 我知道没有错误陷阱等。但是,这是当您使用大量数据透视表和数据透视表时会在最坏的时刻造成严重破坏而没有任何警告的情况下的错误之一。 Thanks. 谢谢。 Jon 乔恩

Changing the title of a source data pivot table to something not ending in 2 numbers seems to prevent the original bug. 将源数据数据透视表的标题更改为不以2个数字结尾的名称似乎可以防止原始错误。 The script I provided above should help you prevent it as long as all of your pivot charts are on the same page as the pivot tables. 只要您所有的数据透视表都与数据透视表位于同一页面上,我上面提供的脚本就应该帮助您防止脚本崩溃。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM