I have a file containing dates on the first row, and a column of identifiers defining a table where I fill in the employee data for the corresponding data (See picture)
I want the dates to be sorted in the chronological order, but I only want to sort the dates, before I fill in the table.
I tried the following code
Sub sortByDates()
Dim sumSheet as Worksheet
Dim toSort as Range
Set sumSheet = ThisWorkbook.ActiveSheet
LastCol = sumsheet.Cells.Find(What:="*", After:=[a1], _
SearchOrder:=xlByColumns,
SearchDirection:=xlPrevious).Column
Set toSort = sumsheet.Rows(1).Resize(1, LastCol - 1).Offset(0, 1)
toSort.Select
ActiveWorkbook.Worksheets("Employees").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Employees").Sort.SortFields.Add Key:=toSort, SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Employees").Sort
.SetRange toSort
.Header = xlGuess
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
End Sub
However the dates moves but don't order as I wish. I think this might have to do with the fact that Excel Sort doesn't know how to handle the dates format, but not sure.
.Header = xlNointstead of xlGuess