Guest User

Untitled

a guest
Oct 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. Sub FixAllPivotTables()
  2.  
  3. Dim pt As PivotTable
  4. Dim ws As Worksheet
  5.  
  6. 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
  7.  
  8. 'change the settings
  9. For Each ws In ActiveWorkbook.Worksheets
  10. '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!
  11. ws.Activate
  12. Cells.Select
  13. ActiveWindow.Zoom = True
  14. For Each pt In ws.PivotTables
  15. 'This one changes the last character of the pivot name to append an "x" so that there are no issues
  16. 'with pivot charts losing their reference when there are >10 pivot tables.
  17. If Right(pt.Name, 1) <> "x" Then
  18. pt.Name = pt.Name + "x"
  19. Debug.Print pt.Name + " I added an x"
  20. Else
  21. Debug.Print pt.Name + " had an x already"
  22. End If
  23. Next pt
  24. ActiveWindow.Zoom = 100
  25. Range("a1").Select
  26. Next ws
  27.  
  28.  
  29. MsgBox "Added an 'x' to the end of each pivot table name if it didn't have one already.", vbOKOnly
  30.  
  31.  
  32. Else
  33. MsgBox "Cancelled", vbOKOnly
  34. End If
  35.  
  36. End Sub
Add Comment
Please, Sign In to add comment