Macro runs slowly but step-by-step is fast

filido

New Member
Joined
Jun 7, 2019
Messages
21
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):

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:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top