Sorting horizontally

tigerzen

Board Regular
Joined
Mar 8, 2023
Messages
165
Office Version
  1. 365
Platform
  1. Windows
Wish to sort the first table so that it produces the second table. I use the MID function to extract the number with which to sort however when I try and sort, it puts Alpha10 in front of Alpha2. I can get this to work if I manually enter the sort numbers, what can I do to the MID function so that its length is one character only rather than the 2 which is what I think is throwing this off. There could well be a better way to handle this sort so any suggestions appreciated.

Book3
ABCDEFGHIJK
1NameP1 AlphaP10 AlphaP12 AlphaP2 AlphaNameP1 AlphaP2 AlphaP10 AlphaP12 Alpha
2Tom3389Tom3938
3Jack24910Jack21049
4Bert9742Bert9274
5Ken31101Ken31110
6To Sort by110122To Sort by121012
Sheet1
Cell Formulas
RangeFormula
B6:E6B6=MID(B1,2,2)
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You could do this easily with a custom sort, something like this perhaps:
VBA Code:
Option Explicit
Sub Custom_Sort()
    Dim ws As Worksheet, Rng As Range
    Set ws = Worksheets("Sheet1")       '<-- *** Change to actual sheet name ***
    Set Rng = ws.Range("B1", Cells(Rows.Count, "E").End(xlUp))
    
    Application.AddCustomList ListArray:=Array("P1 Alpha", "P2 Alpha", "P10 Alpha", "P12 Alpha")
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add2 Key:=Range(ws.Cells(1, 2), ws.Cells(1, 5)), _
        CustomOrder:=Application.CustomListCount
        .SetRange Rng
        .Orientation = xlLeftToRight
        .Apply
    End With
End Sub
 
Upvote 0
. I use the MID function to extract the number with which to sort however when I try and sort, it puts Alpha10 in front of Alpha2
Did you get the box below come up when you tried the sort? and did you choose to sort text as a number?
1693207534840.png


or could you just amend your formula to
Excel Formula:
=--MID(B1,2,2)
 
Upvote 0
Solution
Did you get the box below come up when you tried the sort? and did you choose to sort text as a number?
View attachment 97871

or could you just amend your formula to
Excel Formula:
=--MID(B1,2,2)
Hi Mark, I did get that dialog box and I'm certain that I clicked on the first option but it did not produce the desired result. The original file contained hundreds of columns and unbeknownst to me, some of the helper cells were considered text, no rhyme or reason as to why this was the case. Anyway, thanks for your solution it works well, how does the -- in front of the MID function impact upon it, does it change into a value?
 
Upvote 0
how does the -- in front of the MID function impact upon it, does it change into a value?
It just converts text numbers to real numbers, it also converts True and False to 1 and 0 respectively so you can use them in calculations.
You could use any arithmetic operator to do the converting to number i.e.
Excel Formula:
=MID(B1,2,2)+0
and
Excel Formula:
=MID(B1,2,2)*1
would also work
 
Upvote 0
It just converts text numbers to real numbers, it also converts True and False to 1 and 0 respectively so you can use them in calculations.
You could use any arithmetic operator to do the converting to number i.e.
Excel Formula:
=MID(B1,2,2)+0
and
Excel Formula:
=MID(B1,2,2)*1
would also work
Thanks for the explanation.
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,147
Members
449,098
Latest member
Doanvanhieu

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