Need to improve column delete code

ouadad

Board Regular
Joined
Jun 19, 2005
Messages
213
I wrote a bit of code to compare the sums in three cells and if they are all zero I need to delete the column, but it's achingly slow; like one column every 3 seconds and there are over 5000 columns.

Is there a way to speed up the process?

Code:

Code:
Sub Delete_Columns_with_no_demand_or_forecasts()
Dim iRow As Integer, iColumn As Integer, totalcols As Long
For iColumn = 5159 To 2 Step -1
    If Cells(16, iColumn) + Cells(30, iColumn) + Cells(44, iColumn) = 0 Then
        Columns(iColumn).EntireColumn.Delete
    End If
    Cells(1, 1) = iColumn
Next iColumn
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try
Code:
Sub Delete_Columns_with_no_demand_or_forecasts()
   Dim iRow As Integer, iColumn As Integer, totalcols As Long
   With Application
      .ScreenUpdating = False
      .EnableEvents = False
      .Calculation = xlCalculationManual
   End With
   For iColumn = 5159 To 2 Step -1
       If Cells(16, iColumn) + Cells(30, iColumn) + Cells(44, iColumn) = 0 Then
           Columns(iColumn).EntireColumn.Delete
       End If
       Cells(1, 1) = iColumn
   Next iColumn
   With Application
      .ScreenUpdating = True
      .EnableEvents = True
      .Calculation = xlCalculationAutomatic
   End With
End Sub
 
Upvote 0
I wrote a bit of code to compare the sums in three cells and if they are all zero I need to delete the column, but it's achingly slow; like one column every 3 seconds and there are over 5000 columns.

Is there a way to speed up the process?

Another option:
Assuming there are no blank cell in row 1 in the data set.
Code:
Sub a1078141a()
'https://www.mrexcel.com/forum/excel-questions/1078141-need-improve-column-delete-code.html
Dim r As Range, i As Long, va, vb
 
 va = Range(Cells(1, 1), Cells(44, 5159))
 Set r = Range(Cells(1, 1), Cells(1, 5159))
 vb = r
    For i = 2 To 5159
           If va(16, i) + va(30, i) + va(44, i) = 0 Then
           vb(1, i) = ""
           End If
    Next
 
 r = vb
 r.SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,400
Messages
6,184,762
Members
453,255
Latest member
excelbit

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