Eliminating empty columns - Code optimization.

Pquigrafamos

New Member
Joined
Sep 8, 2021
Messages
18
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello! At a certain point of a bigger macro, I have the need to eliminate all columns that have no filled cells, in the range A to H.
The following piece of code does it perfectly, but it makes the whole macro extremely slow (More than 2 minutes waiting):

VBA Code:
sub El_col1()
Worksheets("Tables").Activate
Dim Filled_cells As Integer
Dim x As Integer
For x = 1 To 8
Filled_cells = Application.WorksheetFunction.CountA(Columns(x))
If Filled_cells = 0 Then
Columns(x).EntireColumn.Delete
End If
Next
End sub

Can you please advise if I am doing something wrong here, if there is a way of optimizing this step?
Best regards!! :)
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
See if this makes enough of a difference.
The theory is when deleting rows work from the bottom up, when deleting columns work from right to left.
Also turn off screen updating and calculations (and back on at the end)

VBA Code:
Sub El_col1()

    Application.ScreenUpdating = False
    Application.Calculation = False
  
    Worksheets("Tables").Activate
    Dim Filled_cells As Integer
    Dim x As Integer
    For x = 8 To 1 Step -1
        Filled_cells = Application.WorksheetFunction.CountA(Columns(x))
        If Filled_cells = 0 Then
            Columns(x).EntireColumn.Delete
        End If
    Next
  
    Application.ScreenUpdating = True
    Application.Calculation = True
End Sub
 
Upvote 0
Solution
@Alex Blakenburg , thanks a lot for the suggestion! :)
I have tried it and unfortunately this change did not improve the speed of the macro, it still adds two minutes to a macro that runs in 2 seconds.
 
Upvote 0
@Alex Blakenburg , I am sorry but I have found out that these deleted columns were associated with other cells in the same tab with formulas.
This is the reason why the macro was dragging so much when the column deleting was applied there.
Still, your code is faster than mine, thanks a lot for the improvement. :D
 
Upvote 0
Can row 1 be used as the criteria for blank column? say, if C1 is blank then column C must be blank
 
Upvote 0
Oops your reference to formulas made me check what I sent you.
Can you please change the calculation lines to
VBA Code:
Application.Calculation = xlManual
' body of code
Application.Calculation = xlAutomatic
 
Upvote 0
Did the second change, once again many thanks @Alex Blakenburg :)
@Akuini here not, unfortunately.
The project's purpose is to clean data extracted from pdf files.
Prior to this step, the section is unmerged, which leaves out sometimes some blank columns.
In this way, there might be just a cell filled somewhere in one of the lines that cannot be erased.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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