**Sorry, header should read: Sort TEXT as Numbers - Excel VBA**
Hello!
I created the code below based on a macro to sort columns by Date first, then by Invoice #, however I notice sometimes that invoices that share the same date are not sorted in ascending order.
? The Invoice column is formatted as text so I think that could be the reason why.
? I'm hoping to find a solution to have the macro consistently sort data in order by Date (oldest to newest) then by Invoice (ascending).
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("D2:D3000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B2:B3000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:F3000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Example of End Results:
Column D Column B
Date Invoice
9/6/2019 2467781
9/6/2019 124555675
2/1/2020 2577775
2/1/2020 125777675
Hello!
I created the code below based on a macro to sort columns by Date first, then by Invoice #, however I notice sometimes that invoices that share the same date are not sorted in ascending order.
? The Invoice column is formatted as text so I think that could be the reason why.
? I'm hoping to find a solution to have the macro consistently sort data in order by Date (oldest to newest) then by Invoice (ascending).
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("D2:D3000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B2:B3000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:F3000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Example of End Results:
Column D Column B
Date Invoice
9/6/2019 2467781
9/6/2019 124555675
2/1/2020 2577775
2/1/2020 125777675