Fastest Way to Delete Columns

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
589
Office Version
  1. 2019
Platform
  1. Windows
I am running a macro that will take a worksheet weekly and drill down on the data.

This worksheet will always have columns A:U. I am deleting a good portion of these with this code:

Code:
Range("B:B,F:F,H:L,N:T").EntireColumn.Delete

Unfortunately this line of code alone takes roughly 7 to 8 seconds to delete these columns.

I am running this to help the speed:

Code:
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Is there a faster way to get the code to run possibly using variables or something that would make it go faster when running?

heres the code:
Code:
Dim i As Integer
Dim minDate As Date
Dim wsName As String




    Application.DisplayAlerts = False
    Application.ScreenUpdating = False




        Sheets("Weekly Static Routes").Select


        Range("B:B,F:F,H:L,N:T").EntireColumn.Delete
        Range("A1") = "TMS #"
        Range("C1") = "Carrier"
        Range("D1") = "Pickup Date & Time"
        Range("E1") = "Shipper"
        Range("F1") = "Consignee"
        Range("D:D").NumberFormat = "mm/dd/yyyy hh:mm"
        Range("E:E").Select
            With Selection
                Selection.NumberFormat = "General"
                .Value = .Value
            End With
        Worksheets("Weekly Static Routes").Range("A:G").Columns.AutoFit
        
        Worksheets("Weekly Static Routes").Select
        ActiveWorkbook.Worksheets("Weekly Static Routes").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Weekly Static Routes").Sort.SortFields.Add Key:= _
                Range("E:E"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
                :=xlSortNormal
            With ActiveWorkbook.Worksheets("Weekly Static Routes").Sort
                .SetRange Range("A1:G10000")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        
        
        ActiveSheet.Range("A:G").AutoFilter Field:=5, Criteria1:="<>1"
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        If lr > 1 Then
            Range("A2:A" & lr).EntireRow.Delete
        End If
        Selection.AutoFilter


thank you,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

mole999

Well-known Member
Joined
Oct 23, 2004
Messages
10,524
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
turn off calculation and then turn it back on, would seem you have formulas over the cells, and each will evaluate
 
Upvote 0

svendiamond

Well-known Member
Joined
Jun 13, 2014
Messages
1,504
Office Version
  1. 365
Platform
  1. Windows
Code:
Application.Calculation = xlCalculationManual
'your code here
Application.Calculation = xlCalculationAutomatic
 
Upvote 0

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
589
Office Version
  1. 2019
Platform
  1. Windows
adding in this code was actually a bit slower.

6.02 seconds without the calculation
6.33 with your code wrapped around mine

any other ideas?
 
Upvote 0

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,874
Office Version
  1. 365
Platform
  1. Windows
If you have Worksheet_Change macro, then try to turn off events: Application.EnableEvents = False and then turn them back on.
 
Upvote 0

svendiamond

Well-known Member
Joined
Jun 13, 2014
Messages
1,504
Office Version
  1. 365
Platform
  1. Windows
Can you just hide the columns rather than delete them?
 
Upvote 0

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
589
Office Version
  1. 2019
Platform
  1. Windows
Sektor - Enablevents ran at 6.1 seconds, which is a little slower

svendiamond - i would really prefer to have all the information i dont need deleted.
 
Upvote 0

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
589
Office Version
  1. 2019
Platform
  1. Windows
if i clear them out, which is an option, i still want all my data sorted out nicely, how would i accomplish that?
 
Upvote 0

Forum statistics

Threads
1,190,615
Messages
5,981,946
Members
439,746
Latest member
VBANewbieJohn

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