Code to delete all empty cells in a range (shift left)

kidwispa

Active Member
Joined
Mar 7, 2011
Messages
330
Hi All,

I'm trying to run a macro that deletes all empty cells in range C1:DX24719) and shifts left but having used this code :

Code:
Sub DeleteCells()
Range("C1:H6").SpecialCells(xlCellTypeBlanks).Delete shift:=xlToLeft
End Sub

However this takes ages and has so far made Excel hang for about 20 mins...

How can I speed it up and why is it so slow?

Thanks for any help (again!)

:)
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
That's 3M cells, and Excel has to adjust formulas for all that survive.

You might try setting calculation to manual and disabling screen updating, but I wouldn't expect any miracles.
 
Upvote 0
That's 3M cells, and Excel has to adjust formulas for all that survive.

You might try setting calculation to manual and disabling screen updating, but I wouldn't expect any miracles.


There aren't any formulas in the range given - what I have is a grid where as few as 1 and as many as 26 cells in each row of the range have either 1 or 2-digit numbers - rather than have big gaps throughout I'm trying to get the information to read left to right...
 
Upvote 0
This runs in a few seconds, but assumes there are no formulas in the cells.

Code:
Sub DeleteCells()
    Dim av          As Variant
    Dim iRow        As Long
    Dim jRead       As Long
    Dim jWrit       As Long
 
    With Range("C1:DX24719")
        av = .Value
        For iRow = 1 To .Rows.Count
            jWrit = 1
            
            For jRead = 1 To .Columns.Count
                If Not IsEmpty(av(iRow, jRead)) Then
                    av(iRow, jWrit) = av(iRow, jRead)
                    If jRead <> jWrit Then av(iRow, jRead) = Empty
                    jWrit = jWrit + 1
                End If
            Next jRead
        Next iRow
 
        .Value = av
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,741
Members
452,940
Latest member
rootytrip

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