View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Daniel Bonallack Daniel Bonallack is offline
external usenet poster
 
Posts: 110
Default how to I sort and keep blank lines in between data

I'm sure someone will have a better solution, but this would be mine...

This code works from highlighting the section (without header rows) that you
want sorted, and assumes that the blank cells have no text at all in them.

It also uses the column next to your selection - you would have to change
the code if you need to use another column.

Sub SortWithBlank()

Dim c As Object
Set c = ActiveCell

For Each c In Selection
If c.Value = "" Then
c.Offset(0, 1).Value = c.Offset(-1, 0).Value & 1
Else
c.Offset(0, 1).Value = c.Value
End If
Next c
Range(Cells(Selection.Row, Selection.Column), Cells(Selection.Row +
Selection.Rows.Count - 1, Selection.Column + 1)).Select
Selection.Sort Key1:=Cells(Selection.Row + 1, Selection.Column + 1),
Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range(Cells(Selection.Row, Selection.Column + 1), Cells(Selection.Row +
Selection.Rows.Count - 1, Selection.Column + 1)).ClearContents
Range("A1").Select

End Sub



"G" wrote:

How do I sort data while keeping a blank line in between the data.

ex.

dogs
x
cats
x