ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how to I sort and keep blank lines in between data (https://www.excelbanter.com/excel-programming/378287-how-i-sort-keep-blank-lines-between-data.html)

G

how to I sort and keep blank lines in between data
 
How do I sort data while keeping a blank line in between the data.

ex.

dogs
x
cats
x

Daniel Bonallack

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



All times are GMT +1. The time now is 04:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com