Getting Acronyms with numbers to sort in number order

TCyber

New Member
Joined
Sep 12, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hello, I am trying to make a pivot table with a large amount of different acronyms/number combinations. The problem is I have numbers like TB-1, TB-11, TB-2(3), DW-5(7), DW-6. I really need them to sort in the number after the dash. So it would look in a column in the order like this TB-1 | TB-1(1) | TB-2..............TB-10 | TB-11 and so on. right now it puts 11 before 2, there are 100's of these I need to verify and this would make it so much easier.

Office 365
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
For your single digit values add a leading zero, e.g TB-1 change to TB-01, …
 
Upvote 0
Is it possible to do that in mass? There are over 300 combinations that I would have to manually put the zero's in.
You should be able to do it with Excel’s Find and Replace functionality using a regular expression.

Are you familiar with regular expressions?
 
Upvote 0
You should be able to do it with Excel’s Find and Replace functionality using a regular expression.

Are you familiar with regular expressions?
I am and I thank you for your help! If I get stuck I will send another post.
 
Upvote 0
Let's say the data is in column A. If you run the macro below, the output will be sorted as desired.

VBA Code:
Sub jec()
 Dim ar, j As Long
 ar = Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
 With CreateObject("vbscript.regexp")
   .Pattern = "(TB-)(\d(?=\(|$))(.*)"
    For j = 1 To UBound(ar)
      ar(j, 2) = .Replace(ar(j, 1), "$10$2$3")
    Next
 End With
 Range("A1", Range("A" & Rows.Count).End(xlUp)) = Application.Index(Application.Sort(ar, 2), , 1)
End Sub


Note: This is not sorted yet

Book1
A
1TB-1
2TB-1(1)
3TB-1(2)
4TB-11
5TB-2
6TB-3
7TB-5
Sheet1
 
Upvote 1
Using an Excel formula:

Book1
AB
1InputOutput
2TB-1TB-1
3TB-1(1)TB-1(1)
4TB-1(2)TB-1(2)
5TB-11TB-2
6TB-2TB-3
7TB-3TB-5
8TB-5TB-11
Sheet1
Cell Formulas
RangeFormula
B2:B8B2=SORTBY(A2:A8,SUBSTITUTE(REPLACE(A2:A8,MMULT(IFERROR(SEARCH({"-?(","-?^"},A2:A8&"^"),0),{1;1})+1,0,0),"0T","T"))
Dynamic array formulas.
 
Upvote 1
One more formula option in column C

Book1
ABC
1InputOutputOutput 2
2TB-1TB-1TB-1
3TB-1(1)TB-1(1)TB-1(1)
4TB-1(2)TB-1(2)TB-1(2)
5TB-11TB-2TB-2
6TB-2TB-3TB-3
7TB-3TB-5TB-5
8TB-5TB-11TB-11
Sheet1
Cell Formulas
RangeFormula
B2:B8B2=SORTBY(A2:A8,SUBSTITUTE(REPLACE(A2:A8,MMULT(IFERROR(SEARCH({"-?(","-?^"},A2:A8&"^"),0),{1;1})+1,0,0),"0T","T"))
C2:C8C2=SORTBY(A2:A8,IF(LEN(SUBSTITUTE(MID(A2:A8,4,2),"(",""))=1,REPLACE(A2:A8,4,0,0),A2:A8))
Dynamic array formulas.
 
Upvote 1
Thank you both for your help is there somewhere that I would be able to use a wildcard in the formula? There are many different letter combinations is it possible to use the formula to get this type of outcome?

Book1
A
1AX-8
2AX-10
3BF-1
4BF-2
5BF2(3)
6BF-20(1)
Sheet1


the letter acro's need to stay in that order so its like all of AB 1 -100 AC 1-100 and so on
 
Upvote 0
How about BF2(3)? Where is the hyphen?
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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