OaklandJim
Well-known Member
- Joined
- Nov 29, 2018
- Messages
- 855
- Office Version
- 365
- Platform
- Windows
Team XLNT
I am trying to do a basic sort with VBA. I have a data range with headers. I tried to adapt code that I found on line but the headers get sorted with the data. Not what I need! My hack-code is shown below. I set the "all data" range needed by getting the current region using one of the headers. What do I need to do to sort the two keys without headers sorting too? I suspect that I need to specify Header:=xlYes but I cannot figure out how?
I am trying to do a basic sort with VBA. I have a data range with headers. I tried to adapt code that I found on line but the headers get sorted with the data. Not what I need! My hack-code is shown below. I set the "all data" range needed by getting the current region using one of the headers. What do I need to do to sort the two keys without headers sorting too? I suspect that I need to specify Header:=xlYes but I cannot figure out how?
VBA Code:
Sub Sort_Names_Main()
Dim rAllData As Range
With [Main]
Set rAllData = .Range("Header_LastName").CurrentRegion
With .Sort
.SortFields.Clear
.SortFields.Add Key:=Range("Header_LastName"), Order:=xlAscending
.SortFields.Add Key:=Range("Header_FirstName"), Order:=xlAscending
.SetRange rAllData
.Apply
End With '.Sort
End With
End Sub