VBA Too Slow

ChrisFoster

Active Member
Joined
Jun 21, 2019
Messages
256
Office Version
  1. 365
Platform
  1. Windows
Morning all,

I have some VBA code that on the face of it isn't too resource heavy, but it takes a lot longer to run than I'd expect.

There are circa 11,000 rows of data though so maybe I'm just being naive with my expectations.

Is there anything I can do to speed this up?

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+u
'

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Sheets("1.Filter-group-1").UsedRange.Copy
Sheets("pfs-Summary.csv").Select
ActiveSheet.Paste
    
    Sheets("1.Filter-group-1").Select
    
    Range("B:D").Delete
    Cells.EntireColumn.AutoFit
    
        Cells.Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    Range("A1").Select

    
        Columns("B:CW").Select
    Application.CutCopyMode = False
    Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
    End With
    
    
    Cells.Replace What:="Original ", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    
Cells.Columns.AutoFit

    Cells.Replace What:="-", Replacement:="0", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
There are a lot of unnecessary selecting and activating. Try this on a copy.

VBA Code:
Sub Macro1()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim sourceRange As Range
    Dim destRange As Range

    Set wsSource = Sheets("1.Filter-group-1")
    Set wsDest = Sheets("pfs-Summary.csv")

    Set sourceRange = wsSource.UsedRange
    Set destRange = wsDest.Range("A1").Resize(sourceRange.Rows.Count, sourceRange.Columns.Count)

    destRange.Value = sourceRange.Value
   
    With wsSource
        .Range("B:D").Delete
        .Cells.EntireColumn.AutoFit

        With .Cells
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With

        .Columns("B:CW").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"

        .Cells.Replace What:="Original ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

        .Cells.Columns.AutoFit

        .Cells.Replace What:="-", Replacement:="0", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    End With

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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