How to sort a column with a range in it...

nniedzielski

Well-known Member
Joined
Jan 8, 2016
Messages
598
Office Version
  1. 2019
Platform
  1. Windows
I was given a large data file and one of the columns has a mileage range in it:
1670291136481.png

by default, Excel is treating it like text, and i cannot figure out how to get excel to sort the column the common sense way which would be:
0-20
21-40
and so on and so on

I tried google and tried changing the values to number, and general, but nothing seems to work, and i do not want to have to go through and split the range up into multiple cells unless i absolutely have to.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If you're open to a VBA solution then the following seems to work. Obviously I have no idea what your data looks like (columns involved, range, sheet name etc.) but if you provide a sample using the XL2BB add in then I'll happily adjust the demonstration code below.
With this code:
VBA Code:
Option Explicit
Sub TestSort()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    Dim LRow As Long, LCol As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    
    With ws.Range(ws.Cells(2, LCol), ws.Cells(LRow, LCol))
        .value = _
        Evaluate("IFERROR(INT(MID(" & .Offset(, -3).Address & ",1,FIND(""-""," _
        & .Offset(, -3).Address & ")-1)),2000)")
    End With
    
    ws.Range("A1").CurrentRegion.Sort key1:=ws.Cells(1, LCol), Order1:=xlAscending, Header:=xlYes
    ws.Cells(1, LCol).EntireColumn.ClearContents
    Application.ScreenUpdating = True
End Sub

This:
Book1
ABC
1header1header2header3
2161-180was row 2was row 2
32000+was row 3was row 3
41501-2000was row 4was row 4
50-20was row 5was row 5
621-40was row 6was row 6
7181-200was row 7was row 7
81001-1500was row 8was row 8
Sheet1


Becomes this:
Book1
ABC
1header1header2header3
20-20was row 5was row 5
321-40was row 6was row 6
4161-180was row 2was row 2
5181-200was row 7was row 7
61001-1500was row 8was row 8
71501-2000was row 4was row 4
82000+was row 3was row 3
Sheet1
 
Upvote 0
Excel is treating it like text
That isn't surprising as the values are text. After all you cannot sum them, find an average etc.

i do not want to have to go through and split the range up into multiple cells unless i absolutely have to.
To me that would be a simple way to go as you only need one column not multiple.
For example this, then sort by the extra column.

22 12 06.xlsm
ABCDE
1Hdr1mileageHdr3Hdr5Sort By
2101-120data 1101
3161-180data 2161
41001-1500data 31001
52001+data 42001
6201-220data 5201
Sort
Cell Formulas
RangeFormula
E2:E6E2=LEFT(B2,FIND("-",SUBSTITUTE(B2,"+","-"))-1)+0
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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