DELETE BLANK CELLS

tran780

New Member
Joined
Apr 17, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'd like to delete rows that are blank, my code below
I have to run the code 4-5 times to get result. Can anyone help to explain for me ?

Sub test()
Dim lastrow2, x As Integer
lastrow2 = Sheets("GPE").Range("A2").CurrentRegion.Rows.Count
For x = 2 To lastrow2
If Sheets("GPE").Cells(x, 4) = "" Then
Sheets("GPE").Rows(x).EntireRow.Delete
End if
Next x
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
When deleting rows you MUST always start at the bottom and work up....your current code is skipping rows, because when a row is deleted everything moves up and a "new" blank row is missed.
Maybe try it this way...IF you cells are truly blank, not blank because of a formula result
VBA Code:
Sub MM1()
  On Error Resume Next
  Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Upvote 0
When deleting rows you MUST always start at the bottom and work up....your current code is skipping rows, because when a row is deleted everything moves up and a "new" blank row is missed.
Maybe try it this way...IF you cells are truly blank, not blank because of a formula result
VBA Code:
Sub MM1()
  On Error Resume Next
  Range("D2", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
That does not specifically refer to the relevant sheet but perhaps more importantly, if there are blanks at the bottom of column D within the relevant worksheet range that code will not delete those rows.
A modified version that addresses those issues is:

VBA Code:
Sub DelRws_1() 
  With Sheets("GPE")
    On Error Resume Next
    .Range("D2:D" & .Range("A2").CurrentRegion.Rows.Count).SpecialCells(xlBlanks).EntireRow.Delete
  End With
End Sub

@tran780
I am wondering how big (number of rows) your "GPE" sheet is? If large, the above code may be relatively slow and a faster deleting method could be provided.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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