Delete Blank Rows

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Delete Blank Rows

Post by MSingh »

Hi,

I'm trying to delete blank rows:
Scenario: Data copied from sheet1 & PasteSpecial to sheet 2.
Data in Sheet2 is in Range A15:I213. The cells in this range is formatted, borders are outlined.

I have tried several other code but it does not delete the blank rows.

Here is an example code adapted:

Sub DeleteEmptyRows() 'Code Not my own
Dim LastRow As Long
Dim r As Long
Dim Counter As Long
Application.ScreenUpdating = False
LastRow = ActiveSheet.Range("A15:A213").Rows.Count + _
ActiveSheet.Range("A15:A213").Rows(1).Row - 1
For r = LastRow To 1 Step -1
If Application.WorksheetFunction.CountA(Rows(r)) = 0 Then
Rows(r).Delete
Counter = Counter + 1
End If
Next r
Application.ScreenUpdating = True
MsgBox Counter & " Empty rows were deleted."
End Sub


Please advise

Thanks again
Mohamed

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Blank Rows

Post by HansV »

If a formula results in an empty string, the value of that cell is "", even after Paste Special > Values. This does NOT count as a blank.
You could clear such cells before deleting the empty rows:

Code: Select all

Sub DeleteEmptyRows() 'Code Not my own
  Dim rng As Range
  Dim LastRow As Long
  Dim r As Long
  Dim Counter As Long
  Application.ScreenUpdating = False
  LastRow = 213
  ' Clear seemingly blank cells
  For Each rng In Range("1:" & LastRow).SpecialCells(xlCellTypeConstants)
    If Trim(rng.Value) = "" Then
      rng.ClearContents
    End If
  Next rng
  ' Delete empty rows
  For r = LastRow To 1 Step -1
    If Application.WorksheetFunction.CountA(Rows(r)) = 0 Then
      Rows(r).Delete
      Counter = Counter + 1
    End If
  Next r
  Application.ScreenUpdating = True
  MsgBox Counter & " Empty rows were deleted."
End Sub
(Your code for LastRow seemed an overly elaborate way to calculate 213, so I omitted it)
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: Delete Blank Rows

Post by MSingh »

Hi Hans,

Thanks again. Works perfectly.

Kind Regards
Mohamed

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Delete Blank Rows

Post by kpark91 »

Or you can just use this code I believe.
Not very sure but..

Code: Select all

Range("A15:A213").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Last edited by HansV on 31 Aug 2010, 14:13, edited 1 time in total.
Reason: to change xlCellTypeBalnks to xlCellTypeBlanks
I don't have one

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Blank Rows

Post by HansV »

The line

Range("A15:A213").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

deletes rows that have a blank in column A. Mohamed wants to delete rows in which ALL cells are blank.
Best wishes,
Hans

kpark91
StarLounger
Posts: 61
Joined: 29 Jul 2010, 14:52

Re: Delete Blank Rows

Post by kpark91 »

Hmm.... Is there a one-liner code for this then?

Would offset work?
I don't have one

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Blank Rows

Post by HansV »

I don't think you can do this with a one-liner.
Best wishes,
Hans