【问题标题】:Pivot chart changes source to different Pivot Table - Microsft Excel Bug数据透视图将源更改为不同的数据透视表 - Microsoft Excel 错误
【发布时间】:2018-03-29 15:40:33
【问题描述】:

我终于自己修复了 Excel 中的错误,有时,数据透视图的源数据看似随机地从“数据透视表 32”变为“数据透视表 3”。

我一直在手工编辑所有这些以文本结尾(这似乎阻止了它的发生。)

我的原始脚本实际上揭示了另一个错误。 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

我知道没有错误捕获等。但是,当您使用大量数据透视表和数据透视图时,这是其中一个错误,它们会在最糟糕的时候造成严重破坏而没有任何警告。谢谢。乔恩

【问题讨论】:

    标签: excel pivot-table pivot-chart vba


    【解决方案1】:

    将源数据透视表的标题更改为不以 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 2018-08-22
      相关资源
      最近更新 更多