Excel Sort only on numerical data

Mickyg2024

New Member
Joined
Dec 14, 2023
Messages
2
Office Version
  1. 365
Good morning,

I am looking for a sort macro that only sorts the numerical data in a cell with the following example string

WF-V-23-0000

I'd like for it to only sort the numerical data from 23-0000 and not the alphabetical data.

thank you
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Example
WF-O-23-0001
WF-O-23-0005
WF-V-23-0001
WF-V-23-0003

I'd like for it to look like this
WF-V-23-0001
WF-O-23-0001
WF-V-23-0003
WF-O-23-0005
 
Upvote 0
Here's this for now, assuming your data is in A1 without headers and the range AA:AB is clear, because I couldn't figure out how to split a string into a 2d array at the moment. So this uses the worksheet to split and sort the values, and then put them back into column A. Hopefully someone else will come by with a solution not needing the worksheet. (I have more learning to do)

VBA Code:
Private Sub NumericSort()
Dim str() As String
Dim i As Long, j As Long, lRow As Long

lRow = Range("A" & Rows.Count).End(xlUp).Row
ReDim str(1 To lRow)

For i = 1 To lRow
    Range("AA" & i & ":AB" & i).Value = Split(WorksheetFunction.Substitute(Range("A" & i).Value, "-", "@", 2), "@")
Next i

Range("AA1:AB" & lRow).Sort key1:=Range("AB1"), order1:=xlAscending, Header:=xlNo

For j = 1 To lRow
    str(j) = Range("AA" & j).Value & "-" & Range("AB" & j).Value
Next j

Range("A1:A" & lRow).Value = Application.Transpose(str)
Range("AA1:AB" & lRow).ClearContents

End Sub
 
Upvote 0
Another option with Formula Excel, Try this

Excel Formula:
=CHOOSECOLS(SORT(HSTACK(LEFT($F$4:$F$7,LEN($F$4:$F$7)-4),RIGHT(F4:F7,4)),2,1),1)
&
CHOOSECOLS(SORT(HSTACK(LEFT($F$4:$F$7,LEN($F$4:$F$7)-4),RIGHT(F4:F7,4)),2,1),2)


1702601997452.png
 
Upvote 0
Another option:
Excel Formula:
=CHOOSECOLS(SORT(HSTACK(A1:A4, VALUE(SUBSTITUTE(RIGHT(A1:A4, 7), "-", ""))), 2, 1, FALSE), 1)
 
Upvote 0
Give this a try...

=LET(s,SORT(A1:A4,,-1),SORT(HSTACK(LEFT(s,5),RIGHT(s,7)),2))
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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