I have a spreadsheet in Excel with three pivot charts. I wrote a VBA code that does the following:
' Copies sheets 1-4
ThisWorkbook.Worksheets(Array("1", "2", "3", "4")).Copy
'Transforms each new sheet data in values only.
For Each iSheet In ActiveWorkbook.Sheets
iSheet.Activate
iSheet.UsedRange.Copy
iSheet.UsedRange.PasteSpecial xlPasteValuesAndNumberFormats
iSheet.Range("A1").Select
Next iSheet
' After pasting, deletes the contents of the cells but keeps the graphs
' in spread sheet 4
ActiveWorkbook.Sheets("4").Columns("J:AB").Clear
This code runs differently in debug mode (F8) compared to when it is run by pressing F5 or running with a button in Excel. In debug mode, it keeps all the series information in the Pivot Charts when the Pivot Tables are pasted as values. When it runs nonstop, it loses the pivot charts information, so that for instance the series names change to defaults 'Series 1', the x values go from specific date values to jan-00, format of x axis labels change to text, etc. I have no idea why. In other very similar question, someone pointed out that PasteSpecial behaves differently when the target worksheet is not activated, which is why I added iSheet.Activate. This difference in action may also be a result of the Clear method of the Pivot Tables.
I tried to add Activate in different steps in the code to no avail.