VBA code running slowly

Coralie2

New Member
Joined
Jan 9, 2018
Messages
1
Hello,

I have a problem with my VBA code, it takes a lot of time to execute even if the code seems quite simple... :confused:
For the context, I have a very large sheet with a pattern of 3 columns (Month, Year and Value) and I want to delete the three of them if the value cell is empty. It has around 1000 columns and 2000 rows. The code works but takes ages, and after a while Excel freezes and stops working :eeek:

Here is my current code :

Code:
Sub del_empty()


Dim NbCol, i As Long
Dim NbRow, j As Long


NbCol = Cells(1, Rows(1).Cells.Count).End(xlToLeft).Column


For i = NbCol To 1 Step -3


NbRow = Cells(Rows.Count, i).End(xlUp).Row
    
    For j = NbRow - 1 To 2 Step -1
        
        If Cells(j + 1, i) = "" Then
        Cells(j + 1, i - 2).Delete
        Cells(j + 1, i - 1).Delete
        Cells(j + 1, i).Delete
        End If
    Next
Next


End Sub

Thank you for reading me ! :bow:

Coralie (I'm french, sorry for my English :oops:)
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Re: VBA code running slowly - please help me !

Welcome to the Board!

Try these minor changes to see if they help:
Code:
Sub del_empty()

Dim NbCol [COLOR=#ff0000]As Long[/COLOR], i As Long
Dim NbRow [COLOR=#ff0000]As Long[/COLOR], j As Long

[COLOR=#ff0000]Application.ScreenUpdating = False[/COLOR]

NbCol = Cells(1, Rows(1).Cells.Count).End(xlToLeft).Column

For i = NbCol To 1 Step -3

NbRow = Cells(Rows.Count, i).End(xlUp).Row
    
    For j = NbRow - 1 To 2 Step -1
        
        If Cells(j + 1, i) = "" Then
        Cells(j + 1, i - 2).Delete
        Cells(j + 1, i - 1).Delete
        Cells(j + 1, i).Delete
        End If
    Next
Next

[COLOR=#ff0000]Application.ScreenUpdating = True[/COLOR]

End Sub
Suppressing the screen updating until the end usually helps speed things up when you are making updates (especially via loops).

Also, it shouldn't really affect the performance, but in declaring your variables, each one need to be explicitly declared, like I show above.
The way you had written it, NbCol and NbRow would have been declared as Variant.
 
Last edited:
Upvote 0
Re: VBA code running slowly - please help me !

Hi,

you find the slowest process in xl!

As your starting point are empty cells, try something like this:

Code:
with activesheet.usedrange.specialcells(4)
   .offset(,-2).clear
   .offset(,-1).clear
   .clear
end with

regards
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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