PDA

View Full Version : [SOLVED:] Filter and Sort with Multiple Headers



pawcoyote
07-20-2019, 11:19 AM
Good afternoon,

I have been reading that this is frowned upon but I have the following setup in a excel spreadsheet and would like to be able to sort it without having the 2nd Header included in the Sort and being moved through out the spreadsheet.

I want to be able to Sort using the Client Name Header. I cannot move the Bottom header above the top header, which everyone is telling me to do. Is there a way to Filter and Sort using the top header without causing the bottom header to move?

24650

Paul_Hossler
07-20-2019, 01:35 PM
Move row 2 above row 1, sort row 2-n, and then put the new row 1 back where it started as row 2

akuini
07-20-2019, 06:26 PM
Hide the second row > sort > unhide

Paul_Hossler
07-21-2019, 04:46 AM
Maybe simpler




Sub Macro1()
Dim r As Range

Set r = ActiveSheet.Cells(1, 1).CurrentRegion
Set r = r.Cells(3, 1).Resize(r.Rows.Count - 1, r.Columns.Count)

With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=r.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange r
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

pawcoyote
07-21-2019, 11:22 AM
Hi,

I tried this and it keeps putting the 2nd header at the bottom of the sort once done.


Maybe simpler




Sub Macro1()
Dim r As Range

Set r = ActiveSheet.Cells(1, 1).CurrentRegion
Set r = r.Cells(3, 1).Resize(r.Rows.Count - 1, r.Columns.Count)

With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=r.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange r
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Paul_Hossler
07-21-2019, 12:03 PM
Probably because 1) I can't count, and 2) I didn't have a good enough test case




Set r = r.Cells(3, 1).Resize(r.Rows.Count - 2, r.Columns.Count)






Option Explicit

Sub Macro1()
Dim r As Range

Set r = ActiveSheet.Cells(1, 1).CurrentRegion
Set r = r.Cells(3, 1).Resize(r.Rows.Count - 2, r.Columns.Count)

With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=r.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange r
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

akuini
07-21-2019, 04:47 PM
Hi, pawcoyote
Did you try my method in post #3?

pawcoyote
07-22-2019, 06:59 AM
Yes, I can do that but that isn't what I want to be able to do. I want to be able to sort from the first header without having to change anything. I know my structure isn't the best but you would think you could do this easily..
Hi, pawcoyote
Did you try my method in post #3?

pawcoyote
07-22-2019, 07:06 AM
Thank you very much for all your help!
Probably because 1) I can't count, and 2) I didn't have a good enough test case




Set r = r.Cells(3, 1).Resize(r.Rows.Count - 2, r.Columns.Count)






Option Explicit

Sub Macro1()
Dim r As Range

Set r = ActiveSheet.Cells(1, 1).CurrentRegion
Set r = r.Cells(3, 1).Resize(r.Rows.Count - 2, r.Columns.Count)

With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=r.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange r
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub