SORT Numbers As TEXT - Excel VBA

NEW_2VBA

Board Regular
Joined
Dec 15, 2015
Messages
106
**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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You will need to convert the text numbers to proper numbers before you can sort like that. That is best done with text to columns with no delimiter. You can use the macro recorder to get reference code if you want to add it to your sort code for future use.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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