Delete Empty Rows is range

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I am trying to delete all the empty rows in a range. What I currently have deletes the rows but skips over a lot as the code runs. Below is what I currently have.

Code:
'msgbox delete blanks???
If MsgBox("Are you sure you want to delete ALL the blank rows in the chart?", vbYesNo, "Delete Blanks?") = vbNo Then
Exit Sub
Else


''''***PCrng is named range = A2:A1000***
For Each cell In Range("PCrng")
    If cell.Value = "" Then
             Set rng2 = cell.Offset(0, 3)
             Range(cell.Offset(0, -1), rng2).Select
             Selection.EntireRow.Delete
           Else
        End If
   Next cell
  Range("a1").Select
 MsgBox "There are no more results for " & myItem
 Exit Sub
End If

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If the cells are truely blank (not formulas returing ""), then try

Code:
Sub DeleteBlanks()
On Error Resume Next
Range("PCrng").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
End Sub
 
Upvote 0
If the cells are NOT truely blank, and contain formulas returing "", then you have to loop backwards (from the last row to the first)

Try
Code:
Sub DeleteBlanks()
With Range("PCrng")
    For i = .Rows.Count To 1 Step -1
        If .Cells(i, 1) = "" Then .Cells(i, 1).EntireRow.Delete
    Next i
End With
End Sub
 
Upvote 0
I am trying to delete all the empty rows in a range. What I currently have deletes the rows but skips over a lot as the code runs. Below is what I currently have.

Code:
'msgbox delete blanks???
If MsgBox("Are you sure you want to delete ALL the blank rows in the chart?", vbYesNo, "Delete Blanks?") = vbNo Then
Exit Sub
Else


''''***PCrng is named range = A2:A1000***
For Each cell In Range("PCrng")
    If cell.Value = "" Then
             Set rng2 = cell.Offset(0, 3)
             Range(cell.Offset(0, -1), rng2).Select
             Selection.EntireRow.Delete
           Else
        End If
   Next cell
  Range("a1").Select
 MsgBox "There are no more results for " & myItem
 Exit Sub
End If

End Sub


This was perfect. Much more efficient as well. Thanks for the help!
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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