PDA

View Full Version : [SOLVED:] Sorting Simplification Problem



vanhunk
09-26-2014, 07:25 AM
Sorting Code needs simplification:

Dear all,

This macro sorts the data in columns B to K according to the date. Dates are in column B. Data starts in row 7 and row 6 has headings The data is in blocks representing months.
Each month has a header or footer row with the first cell the first day of the month with a green background.
It is possible to have a data row for the first day of the month. With the original code, when this is the case, the header row ends up between rows for the same month. This is not acceptable and hence the new code. I would like to simplify the new code. I tried a number of things, but did not succeed.

Please help.

Regards,
vanhunk



Sub OriginalSortingCode()
Dim LaasteCell As Range

Set LaasteCell = Range("B6000").End(xlUp)

Range("B6", Cells(LaasteCell.Row, "K")).Select
Selection.Sort Key1:=Range("B7"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Application.Goto Reference:="'Summary'!R1C1", Scroll:=True

End Sub

New Macro that needs simplification:

Sub NewSortingMacro()
'This macro sorts the data in columns B to K according to the date. Dates are in column B. Data starts in row 7 and row 6 has headings The data is in blocks representing months.
'Each month has a header or footer row with the first cell the first day of the month with a green background.
'It is possible to have a data row for the first day of the month and to prevent the header/footer row to separate this row from the other rows for the same month, I used another criteria, i.e. background colour.

Dim LaasteCell As Range

Set LaasteCell = Range("B6000").End(xlUp)

Range("B6", Cells(LaasteCell.Row, "K")).Select

ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear

ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("B6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("B6"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:=xlSortNormal

With ActiveWorkbook.ActiveSheet.Sort
.SetRange Selection
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With

Application.Goto Reference:="'Summary'!R1C1", Scroll:=True

End Sub

snb
09-26-2014, 02:15 PM
Sub M_snb()
Sheet4.Cells.UnMerge
Sheet4.Cells(6, 2).CurrentRegion.Sort Sheet4.Cells(6, 2), 1, Sheet4.Cells(6, 5), , 2, , , xlYes
End Sub

vanhunk
09-28-2014, 11:58 PM
Hi snb,
I am once again blown away! If you don't mind, could you please explain the syntax of the "sort" line?

Best regards,
vanhunk

snb
09-29-2014, 02:23 AM
There's someone who's better able to explain: VBEditor (Alt-F11), Help (F1), lemma 'range.sort'

vanhunk
09-29-2014, 03:41 AM
Dear snb,
I have tried that but didn't find anything that even vaguely resemble what you have done. I'm sorry I'm clueless.

Regards,
vanhunk

mancubus
09-29-2014, 04:24 AM
http://msdn.microsoft.com/en-us/library/office/ff840646(v=office.15).aspx

this is the syntax:
range.Sort (Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)




Sub M_snb()
Sheet4.Cells.UnMerge
Sheet4.Cells(6, 2).CurrentRegion.Sort Sheet4.Cells(6, 2), 1, Sheet4.Cells(6, 5), , 2, , , xlYes
End Sub


arguments must be written in the syntax order. as you can see some optional arguments are left blank and separated by commas in order to follow the order in the syntax.

range = Sheet4.Cells(6, 2).CurrentRegion
Key1 = Sheet4.Cells(6, 2)
Order1 = 1 (or xlAscending)
Key2 = Sheet4.Cells(6, 5)
Type = ""
Order2 = 2 (or xlDescending)
Key3 = ""
Order3 = ""
Header = xlYes (or 1)
OrderCustom = ""
MatchCase = ""
Orientation = ""
SortMethod = ""
DataOption1 = ""
DataOption2 = ""
DataOption3 = ""


or you can write the same code as below with the arguments stated.:


Sub M_snb()
Sheet4.Cells.UnMerge
Sheet4.Cells(6, 2).CurrentRegion.Sort Key1:=Sheet4.Cells(6, 2), Order1:=xlAscending, Key2:=Sheet4.Cells(6, 5), Order2:=xlDescending, Header:=xlYes
End Sub


ps: if you need to sort your table by more than 3 fields, record a macro.

snb
09-29-2014, 06:11 AM
12326

12327

vanhunk
09-30-2014, 12:44 AM
@snb:
Thank you sir