Sorting by numbers when a cell contains text

NRF95

New Member
Joined
Dec 2, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello! Please, take a look at the image that i uploaded. My goal is to sort the numbers 2021/1, 2021,2 and so on. The problem is that right now they are sorted like 2021/1, 2021/10. I want the sorting to keep in mind that 2022 is larger than 2021, but at the same time to keep in mind thar 10, 11, 12 are larger than 2. What formulas should i use to achieve proper sorting? Thanks in advance.
 

Attachments

  • 6D379BD6-397C-43AE-BD40-10D37EABC4F7.jpeg
    6D379BD6-397C-43AE-BD40-10D37EABC4F7.jpeg
    240.8 KB · Views: 7

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi & welcome to MrExcel.
You could put this in column B & sort on that
Excel Formula:
=IF(LEN(A4)=6,REPLACE(A4,6,0,0),A4)
 
Upvote 0
Hi & welcome to MrExcel.
You could put this in column B & sort on that
Excel Formula:
=IF(LEN(A4)=6,REPLACE(A4,6,0,0),A4)
It worked! Thank you so much, sir! Can you please elaborate on what you did with this if nesting, so that i can use it on my own in similar cases as well in the future?
 
Upvote 0
It checks if the cell has 6 characters & if so it adds a 0 after the /
 
Upvote 0
Try this code , NRF95...

VBA Code:
Public Sub NRF95()
    Const START_ROW = 2, START_COL = 1
    Dim ws As Worksheet, lr As Long, lFormula As String, rFormula As String
    Dim sortL As Range, sortR As Range

    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    lr = ws.Cells(Rows.Count, "A").End(xlUp).Row

    ws.Columns(START_COL + 1).Insert Shift:=xlToRight
    ws.Columns(START_COL + 2).Insert Shift:=xlToRight
    lFormula = "=LEFT(" & Replace(ws.Cells(START_ROW, START_COL).Address, "$", "") & ",5)"
    rFormula = "=RIGHT(" & Replace(ws.Cells(START_ROW, START_COL).Address, "$", "") & ",2)"

    With ws.UsedRange   'Apply Formulas
        .Columns(START_COL + 1).Offset(1).Formula = lFormula
        .Columns(START_COL + 2).Offset(1).Formula = rFormula
        Set sortL = .Columns(START_COL + 1).Offset(1).Resize(lr - 1)
        Set sortR = .Columns(START_COL + 2).Offset(2).Resize(lr - 1)
    End With

    With ws.Sort        'Apply Sort
        With .SortFields
            .Clear
            .Add Key:=sortR
            .Add Key:=sortL
        End With
        .SetRange ws.UsedRange.Offset(1).Resize(lr - 1)
        .Apply
    End With

    ws.Columns(START_COL + 2).Delete    'Remove helper columns (if needed)
    ws.Columns(START_COL + 1).Delete    'Remove helper columns (if needed)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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