Excel VBA Sort on using two columns

Will85

Active Member
Joined
Apr 26, 2012
Messages
254
Office Version
  1. 365
Platform
  1. Windows
Hello,

I need assistance adjusting a macro. I need to sort one value alphabetically, then another value chronologically.

For example, I need

Jon Smith 10/9/2018
Jane Smith 10/31/2018
Jon Smith 9/30/2018

to become

Jane Smith 10/31/2018
Jon Smith 9/30/2018
Jon Smith 10/9/2018

I cant figure out how to define my second set within the same macro. In the below code, C4 and below are my alpha records, D4 and below (which is what I am trying to build into my code) would be my chronological records.

Thank you for your help in advance!

Sub NameSort()
'
' NameSort Macro
'


'
Dim DataFirstCell
Dim DataLastCell
Dim SortCellStart
Dim SortCellEnd

Range("B3").Select
DataFirstCell = ActiveCell.Address
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 4).Select
DataLastCell = ActiveCell.Address

Range("C4").
SortCellStart = ActiveCell.Address
Selection.End(xlDown).Select
SortCellEnd = ActiveCell.Address

ActiveWorkbook.Worksheets("Sorted Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sorted Data").Sort.SortFields.Add Key:=Range(SortCellStart & ":" & SortCellEnd), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sorted Data").Sort
.SetRange Range(DataFirstCell & ":" & DataLastCell)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("B3").Select
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I think I figured it out

Sub NameSort()
'
' NameSort Macro
'


'
Dim DataFirstCell
Dim DataLastCell
Dim SortCellStart
Dim SortCellEnd
Dim SortCellStart2
Dim SortCellEnd2

Range("B3").Select
DataFirstCell = ActiveCell.Address
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 4).Select
DataLastCell = ActiveCell.Address

Range("C4").Select 'Change this column references to change sort Criteria
SortCellStart = ActiveCell.Address
Selection.End(xlDown).Select
SortCellEnd = ActiveCell.Address

Range("D4").Select 'Change this column references to change sort Criteria
SortCellStart2 = ActiveCell.Address
Selection.End(xlDown).Select
SortCellEnd2 = ActiveCell.Address

ActiveWorkbook.Worksheets("Sorted Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sorted Data").Sort.SortFields.Add Key:=Range(SortCellStart & ":" & SortCellEnd), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sorted Data").Sort.SortFields.Add Key:=Range(SortCellStart2 & ":" & SortCellEnd2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sorted Data").Sort
.SetRange Range(DataFirstCell & ":" & DataLastCell)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("B3").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top