Speed Up My Code

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
598
Office Version
  1. 2019
Platform
  1. Windows
Hi guys-

I am running this code on a document with ~ 25,000 rows by66 columns wide. This code right now is taking around 45 seconds to 55 seconds to run, and sometimes I get the (not responding) and it takes over a minute. I was wondering what steps in my code could be adjusted to run faster.
Thank you much,
Code:
Sub PrepareWorkbookRevised()
        
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Dim startTime As Double
    Dim secondsElapsed As Double
    
    startTime = Timer
    
    Sheets("Sheet1").Select
    Rows("1:2").Delete Shift:=xlUp
    Range("A:B,H:H,J:AD,AF:AK,AN:BJ,BL:BN").EntireColumn.Delete
    
    finalrow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For x = 2 To finalrow
    
    If Rows(x).Height <> 30 Then
        Rows(x).RowHeight = 30
    End If
    
    Next x
    
    Cells.Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
        "A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A:J")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    ActiveSheet.Range("A:J").RemoveDuplicates Columns:=2, Header:= _
        xlYes
    Selection.AutoFilter
    ActiveSheet.Range("A:J").AutoFilter Field:=3, Criteria1:="Empty"
    Cells.Select
    Selection.Copy
    Sheets("Empty").Select
    Cells.Select
    ActiveSheet.Paste
    Columns("A:A").EntireColumn.AutoFit
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("E:K").EntireColumn.Delete
    Sheets("Sheet1").Select
    ActiveSheet.Range("A:J").AutoFilter Field:=3, Criteria1:= _
        "Outbound"
    Cells.Select
    Selection.Copy
    Sheets("Outbound").Select
    Cells.Select
    ActiveSheet.Paste
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("D:G,J:J").EntireColumn.Delete
    Sheets("Sheet1").Select
    ActiveSheet.Range("A:J").AutoFilter Field:=3, Criteria1:="Inbound"
    Cells.Select
    Selection.Copy
    Sheets("Inbound").Select
    Cells.Select
    ActiveSheet.Paste
    Cells.Select
    Cells.EntireColumn.AutoFit
    Sheets("Instructions").Select
    
    secondsElapsed = Round(Timer - startTime, 2)
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    MsgBox "Run time was " & secondsElapsed & " seconds", vbInformation
        
End Sub
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Low-hanging fruit: Replace the loop with

Code:
  Range(Rows(2), Rows(finalrow)).RowHeight = 30
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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