Problem when remove blank cells from a range

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hello

I have a range where some of the rows are blank, and sometimes there are two or more in a row without data.

The code that I have remove one blank cell range, but it also skip the next row because the loop doesn't "know" that one row has been removed.

I also have to add that I don't want the entire row to be deleted, only the range .
Code:
Sub Delete_Row()
    
    Dim temp As Long, x As Long

temp = Sheets("1HourDATA").Range("C65536").End(xlUp).Row

For x = 2 To temp
Cells(x, 3).Select
If Cells(x, 3) = "" Or Cells(x, 3) = 0 Then
Range("C" & x & ":L" & x).Select
Selection.Delete Shift:=xlUp
End If
Next x
End Sub

Regards
Espen
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You have to go backwards (bottom to top) when deleting rows

Change
For x = 2 To temp
to
For x = temp to 2 Step -1


Same applies to deleting columns (you would go Right to Left)
 
Upvote 0
You need to loop backwards

Code:
Sub Delete_Row()
    
    Dim temp As Long, x As Long

temp = Sheets("1HourDATA").Range("C65536").End(xlUp).Row

For x = temp To 2 Step -1
    If Cells(x, 3) = "" Or Cells(x, 3) = 0 Then
        Range("C" & x & ":L" & x).Delete Shift:=xlUp
    End If
Next x
End Sub
 
Upvote 0
Thanks guys!!!! That was fast.

Great minds think alike they say :)
 
Upvote 0

Forum statistics

Threads
1,203,625
Messages
6,056,381
Members
444,862
Latest member
more_resource23

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