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
Delete Blank Rows
-
- Administrator
- Posts: 77253
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Delete Blank Rows
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:
(Your code for LastRow seemed an overly elaborate way to calculate 213, so I omitted it)
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
Regards,
Hans
Hans
-
- 3StarLounger
- Posts: 366
- Joined: 12 May 2010, 06:49
Re: Delete Blank Rows
Hi Hans,
Thanks again. Works perfectly.
Kind Regards
Mohamed
Thanks again. Works perfectly.
Kind Regards
Mohamed
-
- StarLounger
- Posts: 61
- Joined: 29 Jul 2010, 14:52
Re: Delete Blank Rows
Or you can just use this code I believe.
Not very sure but..
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
Reason: to change xlCellTypeBalnks to xlCellTypeBlanks
I don't have one
-
- Administrator
- Posts: 77253
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Delete Blank Rows
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.
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.
Regards,
Hans
Hans
-
- StarLounger
- Posts: 61
- Joined: 29 Jul 2010, 14:52
Re: Delete Blank Rows
Hmm.... Is there a one-liner code for this then?
Would offset work?
Would offset work?
I don't have one
-
- Administrator
- Posts: 77253
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands