Delete empty rows in list of data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to delete empty rows in a data list by macro.

Which is the simplest method of doing this
 
One way is to sort the list. Empty rows end up at the top or bottom
depending on which way you sort.
 
Unfortunately the data is in groups and I would mix up the data that I want
to extract by a macro.

Charlie
 
The following uses column A to determine the depth of the data, and scan all
rows from the bottom to top, removing rows where every column in that row is
empty. If cells contain spaces then this will not work. So use version 2
below.

Sub exempty()
Dim xlr As Long, xr As Long
xlr = Cells(Rows.Count, 1).End(xlUp).Row
For xr = xlr To 1 Step -1
If Application.WorksheetFunction.CountA(Rows(xr)) = 0 Then
Rows(xr).EntireRow.Delete
Next xr
End Sub

' code if cells might contain spaces which should be ignored
Sub exempty2()
Dim xlr As Long, xr As Long, xlDelete As Boolean, xc As Integer
xlr = Cells(Rows.Count, 1).End(xlUp).Row
For xr = xlr To 1 Step -1
xlDelete = True
For xc = 1 To 256
If Len(Trim(Cells(xr, xc))) > 0 Then
xlDelete = False
Exit For
End If
Next xc
If xlDelete Then Rows(xr).EntireRow.Delete
Next xr
End Sub
 
sswcharlie1 said:
I want to delete empty rows in a data list by macro.
Sub cleanline()
Dim rg As Range, rgBlank As Range

Set rg = ActiveSheet.Range("A:A")

On Error Resume Next
Set rgBlank = rg.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0

If rgBlank Is Nothing Then 'no blank cell
MsgBox "No Blank cells found"
Else 'else delete entire rows
rgBlank.EntireRow.Delete
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top