Help With Sort

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,836
Office Version
  1. 365
Platform
  1. Windows
I have a sheet where I want to sort either by column AK (then other criteria) or a selection of rows. I want to do it by lowest to highest but the way its working is because one number starts with 1 its putting that first like below. There will be many other permutations also.

Core Dims: L110 x D103 x W99 mm
Core Dims: L125 x D79 x W88 mm
Core Dims: L86 x D102 x W86 mm
Core Dims: L98 x D63 x W98 mm


This is how I want it

Core Dims: L86 x D102 x W86 mm
Core Dims: L98 x D63 x W98 mm
Core Dims: L110 x D103 x W99 mm
Core Dims: L125 x D79 x W88 mm


If with the above example and I select those 4 rows and I do A-Z it gives top result, if I do Z-A that's even worse.

Any ideas please?
 
Your problem is that in your head, these are numbers. But in Excel it is text. You either need to change the way you do data entry, then easily use AutoFilter to sort:

$scratch.xlsm
ABC
1L (mm)D (mm)W (mm)
211010399
31257988
48610286
5986398
sort dimensions


or add more columns to break out the numbers you want to sort, then use AutoFilter to sort

$scratch.xlsm
ABCD
1L (mm)D (mm)W (mm)
2Core Dims: L110 x D103 x W99 mm11010399
3Core Dims: L125 x D79 x W88 mm1257988
4Core Dims: L86 x D102 x W86 mm8610286
5Core Dims: L98 x D63 x W98 mm986398
sort dimensions
Cell Formulas
RangeFormula
B2:B5B2=VALUE(SUBSTITUTE(INDEX(TEXTSPLIT($A2," "),3),"L",""))
C2:C5C2=VALUE(SUBSTITUTE(INDEX(TEXTSPLIT($A2," "),5),"D",""))
D2:D5D2=VALUE(SUBSTITUTE(INDEX(TEXTSPLIT($A2," "),7),"W",""))
 
Upvote 0
hhhhhh.xlsx
A
1Core Dims: L110 x D103 x W99 mm
2Core Dims: L125 x D79 x W88 mm
3Core Dims: L86 x D102 x W86 mm
4Core Dims: L98 x D63 x W98 mm
5
6
7
8Core Dims: L86 x D102 x W86 mm
9Core Dims: L98 x D63 x W98 mm
10Core Dims: L110 x D103 x W99 mm
11Core Dims: L125 x D79 x W88 mm
Sort
Cell Formulas
RangeFormula
A8:A11A8=SORTBY($A$1:$A$4,MATCH("L"&SORT(--TRIM(TEXTBEFORE(TEXTAFTER($A$1:$A$4,": L"),"x"))),"L"&(--TRIM(TEXTBEFORE(TEXTAFTER($A$1:$A$4,": L"),"x"))),0))
Dynamic array formulas.
 
Upvote 0
If sorting by the first length only and your data is always formatted that way then maybe just:
Excel Formula:
=LET(r,A1:A4,SORTBY(r,--TEXTBEFORE(TEXTAFTER(r,": L",1),"x",1),1))

However, I do agree with @6StringJazzer, if your data allows then it would be good to split it out.
 
Upvote 0
I have a sheet where I want to sort either by column AK (then other criteria) or a selection of rows
OK to use a formula and have the result in a different place, as above?

Or do you want the data to be sorted in place without having to make a copy of it?

If the latter then another option is VBA.
 
Upvote 0
CORRECTION
Excel Formula:
SORTBY($A$1:$A$4,MATCH("L"&(--TRIM(TEXTBEFORE(TEXTAFTER($A$1:$A$4,": L"),"x"))),"L"&SORT(--TRIM(TEXTBEFORE(TEXTAFTER($A$1:$A$4,": L"),"x"))),0))
OR
Excel Formula:
=LET(S,--TRIM(TEXTBEFORE(TEXTAFTER($A$1:$A$4,": L"),"x")),x,$A$1:$A$4,SORTBY(x,S))
 
Upvote 0
OK to use a formula and have the result in a different place, as above?

Or do you want the data to be sorted in place without having to make a copy of it?

If the latter then another option is VBA.
Sorry for the late reply. Yes I think VBA is the answer as I don't really want helper columns or copies etc...
 
Upvote 0
You could try the below, it currently sorts from AK2 to the last used row in AK:
VBA Code:
Sub Sorter()
    Dim rng As Range, var As Variant, rAddr As String
    
    Set rng = Range("AK2", Cells(Rows.Count, "AK").End(xlUp)) ' change range to suit
    rAddr = rng.Address(, , , 1)
    var = Evaluate("SORTBY(" & rAddr & ",--TEXTBEFORE(TEXTAFTER(" & rAddr & ","": L"",1),""x"",1),1)")
    rng = var
End Sub
 
Upvote 0
You could try the below, it currently sorts from AK2 to the last used row in AK:
VBA Code:
Sub Sorter()
    Dim rng As Range, var As Variant, rAddr As String
   
    Set rng = Range("AK2", Cells(Rows.Count, "AK").End(xlUp)) ' change range to suit
    rAddr = rng.Address(, , , 1)
    var = Evaluate("SORTBY(" & rAddr & ",--TEXTBEFORE(TEXTAFTER(" & rAddr & ","": L"",1),""x"",1),1)")
    rng = var
End Sub
Thanks, what if I want to add other criteria. i.e sort by AK then AE then C etc...?
 
Upvote 0
That would depend on what was in AE & C and whether you wanted to search by part of that string or the whole cell value.

Might be best to show us more complete sample of what you want to do and the size of the range to be sorted.
 
Upvote 0

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