Slow VBA - Need Help Speeding it Up

Bkisley

Board Regular
Joined
Jan 5, 2017
Messages
94
Below is my current code. It works exactly as I need after a ton of different changes I have made. The issue is...the code takes like 30 seconds to run. It is not a dealbreaker however it is not very efficient. Any ideas on how I can speed this up??

VBA Code:
Sub CreateInvoice()
    Range("A11:u500").Select
    Selection.Copy
    Range("B12").Select
    Selection.End(xlDown).Select
    Sheets("Invoice").Select
    Range("z9").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    Rows("10:514").EntireRow.AutoFit

Dim c As Range

    For Each c In Range("A10:A510").Cells
        If c.Value = "True" Then
            c.EntireRow.Hidden = True

        End If
    Next c
    
    Range("AA1:AL1").Select
    Selection.Copy
    Sheets("Invoice History").Select
    Range("B4").Select
    Selection.End(xlDown).Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Sheets("Invoice").Select
    
    Range("B2:D6").Select
    
End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
12,496
Office Version
  1. 365
Platform
  1. Windows
Using Select and Selection is a good way to slow things down, as such, removing them will speed it up.

I've tried to keep this in line with what your code looks like it is doing but it is untested.
VBA Code:
Sub CreateInvoice()
With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .Calculation = xlManual
End With
    Range("A11:U500").Copy
With Sheets("Invoice")
    .Range("z9").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    .Rows("10:514").EntireRow.AutoFit

        Dim c As Range
    For Each c In .Range("A10:A510").Cells
        If c.Value = "True" Then
            c.EntireRow.Hidden = True
        End If
    Next c
    .Range("AA1:AL1").Copy
End With
With Sheets("Invoice History")
    .Range("B4").End(xlDown).End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End With
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .Calculation = xlAutomatic
End With
    
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,128,128
Messages
5,628,862
Members
416,345
Latest member
sayad

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
Top