Hello everyone,
I am trying to create a custom sorting in VB. My sorting needs to be first on column B, then C and last on G. My data does not have headers.
How do I create such a custom sorting?
Thanks in advance?
Hello everyone,
I am trying to create a custom sorting in VB. My sorting needs to be first on column B, then C and last on G. My data does not have headers.
How do I create such a custom sorting?
Thanks in advance?
Last edited by sentinela; 01-11-2010 at 05:59 PM. Reason: Solved
Record a macro going through each of the subsequent sorting steps. Post the code and workbook for help on cleaning up the code.
Palmetto
Do you know . . . ?
You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.
Palmetto,
Thanks for the answer, here is the code recorded.
Thanks for the help!![]()
Sub Macro2() ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Add Key:=Range( _ "B1:B3249"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Add Key:=Range( _ "C1:C3249"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortTextAsNumbers ActiveWorkbook.Worksheets("Readings").Sort.SortFields.Add Key:=Range( _ "G1:G3249"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortTextAsNumbers With ActiveWorkbook.Worksheets("Readings").Sort .SetRange Range("A1:I3249") .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ActiveWindow.SmallScroll Down:=-12 Selection.EntireRow.Insert Range("A1").Select End Sub
Maybe like this:
If you create a dynamic named range for your data, you could avoid the hard-coded references:![]()
Sub Macro2() With ActiveWorkbook.Worksheets("Readings").Sort .SortFields.Clear .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SetRange Range("A1:I3249") .SortFields.Add Key:=Range("B1:B3249"), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortNormal .SortFields.Add Key:=Range("C1:C3249"), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortTextAsNumbers .SortFields.Add Key:=Range("G1:G3249"), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortTextAsNumbers .Apply End With End Sub
![]()
Sub Macro2() Dim rSort As Range Set rSort = Range("myData") With ActiveWorkbook.Worksheets("Readings").Sort .SortFields.Clear .Header = xlNo .MatchCase = False .Orientation = xlTopToBottom .SetRange rSort .SortFields.Add Key:=rSort.Columns(2), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortNormal .SortFields.Add Key:=rSort.Columns(3), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortTextAsNumbers .SortFields.Add Key:=rSort.Columns(7), _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortTextAsNumbers .Apply End With End Sub
Entia non sunt multiplicanda sine necessitate
In conjunction with shg's suggestion to use a dynamic named range, see this link for help on the topic.
Thank you very much shg and palmetto.
I appreciate all your help!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks