VBA sorting excel data in correct numerical order.

I have an excel spreadsheet with a column named ID. The ID has the following data:

ID_1
ID_1.5
ID_2
ID_3.5
ID_4
ID_11
ID_22
ID_113

I have put this data in the correct order, but when I use excel to sort the data I get the following result:

ID_1
ID_1.5
ID_11
ID_113
ID_2
ID_22
ID_3.5
ID_4

I can see what and why this is happening, but is there a way using VBA to sort a worksheet by a column and order this in the more intuitive way (as per first example) while taking the ID_ prefix into account.
Blowfelt82Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
If ALL values in that column are in the format "ID_<number>", no VBA is needed.

Just add a column to your data, and use a formula like this to isolate the numeric portion:

=VALUE(MID(A2,4,100))

Open in new window


and then use that column in your sort criteria.
0
Rgonzo1971Commented:
HI,

Why not use a helper'scolumn to sort with

=Right(A1,Len(A1)-3)

Regards
0
Rgonzo1971Commented:
HI,

pls try
Sub macro()
    Range("B2:B" & Range("A2").End(xlDown).Row).Formula = "=Right(A2,Len(A2)-4)"
    Range("B2:B" & Range("A2").End(xlDown).Row).Value = Range("B2:B" & Range("A1").End(xlDown).Row).Value
    Range("B2:B" & Range("A2").End(xlDown).Row).Sort Range("B2")
    Range("B2:B" & Range("A2").End(xlDown).Row).EntireColumn.Delete
End Sub

Open in new window

See example
EE20151119_1.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DougCommented:
Another option, if "ID" is unnecessary, insert a column to the right of your ID column. Then use the text to column button on the Data tab under the Data Tools group. Select the "_" (underscore) character as your delimiter. Now you have numerical values that can be sorted based on the new column. No VBA necessary.

Text to columns graphic
1
Excel amusantCommented:
Hi,

Another solution!
If the name ID is always the same and you have the list of name IDs then you can create a custom list and sort data by custom list.


An example file attached.



Best,
--crp.unops.local-files-UserHome-Sh.xlsx
--crp.unops.local-files-UserHome-Sha.PNG
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.