Hi all,
I am trying to filter data from a table (not pivot table) and then move boxplot charts and scatter charts to different sheets. I tried to do this by looping at the beginning but I don't know how since every "category" is named differently and also I want to copy charts as pictures and name them as I paste them to different sheets. Very rarely my macro runs in 5 minutes but 9/10 times it takes over 60 minutes to complete. But if I run it step-by-step, it works perfectly. Hope someone can help me.
Here is part of my code (there are actually 10 categories so it is really long and heavy):
I am trying to filter data from a table (not pivot table) and then move boxplot charts and scatter charts to different sheets. I tried to do this by looping at the beginning but I don't know how since every "category" is named differently and also I want to copy charts as pictures and name them as I paste them to different sheets. Very rarely my macro runs in 5 minutes but 9/10 times it takes over 60 minutes to complete. But if I run it step-by-step, it works perfectly. Hope someone can help me.
Here is part of my code (there are actually 10 categories so it is really long and heavy):
Code:
Sub CreateCharts()
'
' CreateCharts Macro
'
'
'Speed up the macro
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
'Filter data
ThisWorkbook.Sheets("DATA").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
"Chart 01"
ThisWorkbook.Sheets("Charts").ChartObjects("C1").CopyPicture
Application.Goto Sheets("Sheet1").Range("B2")
ActiveSheet.Pictures.Paste.Name = ("Sheet1_C1")
ThisWorkbook.Sheets("Charts").ChartObjects("C2").CopyPicture
Application.Goto Sheets("Sheet1").Range("B39")
ActiveSheet.Pictures.Paste.Name = ("Sheet1_C2")
'Refresh calculation on the data of scatter plots
ThisWorkbook.Worksheets("Data_Cust").Calculate
ThisWorkbook.Worksheets("Data_Prod").Calculate
'Activate data labels
Sheets("Charts").ChartObjects("C3").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C3").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
Sheets("Charts").ChartObjects("C4").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C4").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
Sheets("Charts").ChartObjects("C5").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C5").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
ThisWorkbook.Sheets("Charts").ChartObjects("C3").CopyPicture
Application.Goto Sheets("Sheet1").Range("X2")
ActiveSheet.Pictures.Paste.Name = ("Sheet1_C3")
ThisWorkbook.Sheets("Charts").ChartObjects("C4").CopyPicture
Application.Goto Sheets("Sheet1").Range("X42")
ActiveSheet.Pictures.Paste.Name = ("Sheet1_C4")
ThisWorkbook.Sheets("Charts").ChartObjects("C5").CopyPicture
Application.Goto Sheets("Sheet1").Range("X80")
ActiveSheet.Pictures.Paste.Name = ("Sheet1_C5")
'
'
'
'
ThisWorkbook.Sheets("DATA").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
"Chart 02"
ThisWorkbook.Sheets("Charts").ChartObjects("C1").CopyPicture
Application.Goto Sheets("Sheet1").Range("B2")
ActiveSheet.Pictures.Paste.Name = ("Sheet2_C1")
ThisWorkbook.Sheets("Charts").ChartObjects("C2").CopyPicture
Application.Goto Sheets("Sheet2").Range("B39")
ActiveSheet.Pictures.Paste.Name = ("Sheet2_C2")
'Refresh calculation on the data of scatter plots
ThisWorkbook.Worksheets("Data_Cust").Calculate
ThisWorkbook.Worksheets("Data_Prod").Calculate
'Activate data labels
Sheets("Charts").ChartObjects("C3").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C3").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
Sheets("Charts").ChartObjects("C4").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C4").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
Sheets("Charts").ChartObjects("C5").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C5").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
ThisWorkbook.Sheets("Charts").ChartObjects("C3").CopyPicture
Application.Goto Sheets("Sheet2").Range("X2")
ActiveSheet.Pictures.Paste.Name = ("Sheet2_C3")
ThisWorkbook.Sheets("Charts").ChartObjects("C4").CopyPicture
Application.Goto Sheets("Sheet2").Range("X42")
ActiveSheet.Pictures.Paste.Name = ("Sheet2_C4")
ThisWorkbook.Sheets("Charts").ChartObjects("C5").CopyPicture
Application.Goto Sheets("Sheet2").Range("X80")
ActiveSheet.Pictures.Paste.Name = ("Sheet2_C5")
'
'
ThisWorkbook.Sheets("DATA").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=8, Criteria1:= _
"Chart 03"
ThisWorkbook.Sheets("Charts").ChartObjects("C1").CopyPicture
Application.Goto Sheets("Sheet3").Range("B2")
ActiveSheet.Pictures.Paste.Name = ("Sheet3_C1")
ThisWorkbook.Sheets("Charts").ChartObjects("C2").CopyPicture
Application.Goto Sheets("Sheet3").Range("B39")
ActiveSheet.Pictures.Paste.Name = ("Sheet3_C2")
'Refresh calculation on the data of scatter plots
ThisWorkbook.Worksheets("Data_Cust").Calculate
ThisWorkbook.Worksheets("Data_Prod").Calculate
'Activate data labels
Sheets("Charts").ChartObjects("C3").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C3").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
Sheets("Charts").ChartObjects("C4").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C4").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
Sheets("Charts").ChartObjects("C5").Activate
ActiveChart.ApplyDataLabels
ActiveSheet.ChartObjects("C5").Activate
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowRange = False
Selection.ShowRange = True
Selection.AutoText = True
Application.CommandBars("Format Object").Visible = False
ThisWorkbook.Sheets("Charts").ChartObjects("C3").CopyPicture
Application.Goto Sheets("Sheet3").Range("X2")
ActiveSheet.Pictures.Paste.Name = ("Sheet1_C3")
ThisWorkbook.Sheets("Charts").ChartObjects("C4").CopyPicture
Application.Goto Sheets("Sheet3").Range("X42")
ActiveSheet.Pictures.Paste.Name = ("Sheet3_C4")
ThisWorkbook.Sheets("Charts").ChartObjects("C5").CopyPicture
Application.Goto Sheets("Sheet3").Range("X80")
ActiveSheet.Pictures.Paste.Name = ("Sheet3_C5")
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End Sub
Last edited by a moderator: