selecting cells with a certain value and deleting and **** to the left

EmyrPC

New Member
Joined
Jan 12, 2018
Messages
9
Hey hey everyone,

I wonder if anyone can help. I have a range (about 11 columns and potentially thousands of rows) which I've copied a formula which could give a result of "" into then copied and pasted as values. Simple enough.

But what I need to do now is delete the "blank" cells and replace the cell with the one to the left. Unfortunately I can't just use xlCellTypeBlanks because Excel it it's wisdom doesn't except "" as a blank cell.

I have been using this code
Range("U6:AS" & Range("A6").End(xlDown).Row).Replace What:="", Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=FalseDim i As Long
Dim j As Long
Range("U6:AE" & Range("A6").End(xlDown).Row).Select
For i = Selection(Selection.Count).Row To Selection.Cells(1, 1).Row Step -1
For j = Selection(Selection.Count).Column To Selection.Cells(1, 1).Column Step -1
If Cells(i, j).Value = 0 Then Cells(i, j).Delete shift:=xlShiftToLeft
Next j
Next i

but as the range is so huge this dramatically increases the time the code takes to run.

Any help is much appreciated.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I don't really know which cells you actually want to work on, but from using your code you are selecting over 1 million rows. Wouldn't something like this be a little more straightforward.

If this does not work for you perhaps you could better explain what the range of cells you want to deal with is.

Code:
    Range("U6:AS" & Range("A6").End(xlDown).Row).Replace What:="", _
    Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
    Dim i As Long, j As Long, lRow As Long
    lRow = Cells(Rows.Count, 21).End(xlUp).Row
    For i = lRow To 1 Step -1
        For j = 41 To 21 Step -1
            If Cells(i, j).Value = 0 Then Cells(i, j).Delete shift:=xlShiftToLeft
        Next j
    Next i
 
Upvote 0
I am glad that it worked for you. I was happy to help. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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