Forcing Excel to Sort Cells as Text

Written by Allen Wyatt (last updated September 20, 2021)
This tip applies to Excel 2007, 2010, 2013, and 2016


12

Kris works in an aerospace factory where they make gas turbine engine parts for many different companies. Each company has their own unique part numbers. Some are all numeric, but most are a combination of numbers and letters of varying lengths. When sorting by part number, Kris would like the column to be treated as text so the sorting comes out as he needs. He tried formatting the cells as text, but that doesn't always help. Also, he's not loving the idea of entering an apostrophe in each cell. Kris wonders if there is a simple way to make Excel sort all the part numbers as text.

Perhaps the most telling part of Kris' problem is the statement that he tried formatting the cells as text, but that doesn't always work. There is a trick to formatting the cells as text—you must do it before you enter information into the cells. If you try to format as text a cell containing a number, that doesn't change the number to text. However, if the cell is formatted as text before you enter the number, then the number is treated as text upon entry.

The solution is to re-enter all your numbers after you format your cells as text. This, of course, could be a major chore when you may be dealing with hundreds or thousands of part numbers. Instead, you could try the following general steps in which I assume the part numbers are in column A:

  1. Insert a column just to the right of your part numbers. (This new column is now column B.)
  2. Format column B as text.
  3. Select column A and press Ctrl+C. This copies the part numbers to the Clipboard.
  4. Select cell B1.
  5. Use Paste Special to paste only the values to the new column.

When you complete these steps, everything in column B should be treated as text and you can delete column A. You should now be able to sort by the part numbers and get the desired results.

There is a problem with this, of course. If you do a lot of copying and pasting of part numbers in the future, it is possible that all of the part numbers may no longer be treated as text. This occurs because if you use the traditional Ctrl+C and Ctrl+V to copy and paste, Excel pastes formatting into the cells as well. Thus, if the part number being pasted was previously formatted as a number, then it will be treated as a number after it is pasted.

In this case you could, if desired, use a macro to make sure your part numbers are always formatted as text. The following is just a short one that will work great:

Sub MakeText()
    Dim c As Range

    Selection.NumberFormat = "@"
    For Each c In Selection
        c.Value = c.Value
    Next c
End Sub

The idea behind the macro is that you would select all the part numbers and then run it. The formatting of the selection is set to text and then each cell within the selection is "re-entered" so that numeric values are subsequently treated as text.

If you are not adverse to using helper columns and you don't want to use a macro, you might try creating one that contains the textual equivalent of your part numbers. This could be done with any of the following formulas:

=A1 & ""
=CONCATENATE(A1)
=TEXT(A1, "@")

Copy the formula down as many cells as necessary. These formulas force the contents of the helper column to be treated as text. You could then sort your part numbers based on the contents of the helper column.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (4627) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Hiding Graphics when Filtering

Excel allows you to set up graphics so that they are associated with cells and even stay with the cells when the cells ...

Discover More

Importing a Text File and Inserting after a Bookmark

Word macros are a great way to automate some of the ways in which you create documents. If you have a need to insert the ...

Discover More

Repeating Table Rows with Manual Page Breaks

Need to make sure part of a table is on one page and part on another? The way to do so is not to use manual page breaks, ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Separating Cells Based on Text Color

If the font color used for the data in your worksheet is critical, you may at some time want to move cells that use a ...

Discover More

Understanding Ascending and Descending Sorts

When you sort information, Excel follows a set pattern of how your data is organized. This tip illuminates the burning ...

Discover More

Automatically Sorting as You Enter Information

When entering information into a worksheet, you may want it to always be in a correctly sorted order. Excel allows you to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 0?

2021-07-01 12:41:32

Willy Vanhaelen

As long as your selection is a single range, you don't need a loop:

Sub Maketext2()
Selection.NumberFormat = "@"
Selection = Selection.Value
End Sub


2021-07-01 11:07:41

Rickard Jonsson

Hello!

I have had similar issues entering different number series into Excel (not formatted as text), and discovered a problem. When entering (as an example) “61-10” Excel converts the data to “oct-61”. If I then copy/past that data into a text formatted cell, it gives the result “22555”.

The easiest way seems to be to remember to start with a text formatted sheet:)

(Swedish version of Excel)


2020-04-29 04:33:05

Mike Barrett

@Alan
Thanks for coming back to me.
I found the answer on this, which you have now touched on.
It didn't matter if I copied or entered the values into text formatted column; whatever I did, clicking Sort did it wrong... until...

The answer was to do what you've said... go to Custom Sort. Excel then asks (very poorly IMO) how you want to sort the values.
By selecting "Sort numbers ad numbers stored as text separately", it sorts the column the way we want it to.

But more than that. Once this is done, this remains the default for future sorting (for text formatted columns). I tried again this morning with a new workbook and entered a column of values and simply clicked the Sort button. It did it according to the selection I made yesterday!
But yes... must format the column as Text first.

Sorted!

Thanks
Mike


2020-04-29 02:52:02

Alex B

@Mike - if you made the column text and MANUALLY entered in the numbers, you should be able to sort it the way you want it. If you "copied" in the numbers all bets are off.

1) check the figures are indeed "all" text with =ISTEXT(CellRef).
If you want to convert them to text there are lots of function that will do it, I usually just use =TRIM(CellRef). You can also convert it using Text to Columns and requesting the output to be text.

2) If they are definitely are all text then when you get the sort options, choose the 2nd one
"Sort numbers and numbers stored as text separately"


2020-04-28 08:46:37

Mike Barrett

Hi

Excel Pro 2010

Even when I open a new sheet and format the column as text and then enter the following list...
755008
755006
755013
755010D
755002
755006D
755010

The sort result is...
755002
755006
755008
755010
755013
755006D
755010D

I need...
755002
755006
755006D
755008
755010
755010D
755013

Google sheets does it correctly.
How can we get Excel to do it right?

I even tried your paste values method but does not work.

Regards
Mike


2017-10-17 18:26:12

Peter Atherton

Many years ago I worked for an aerospace company and we used parts with nunmbers like, GTS123-23, B1234, BA9876 and 7010234, and 77021234. These naturally sort with numbers first so I assumed that Kris needed the Text Values to sort first and then the numbers. My solution would be to use a helper with a lower case 'z' prefix and sort on that. You could use a macro to create the prefix, something like:

Sub AddPrefix()
Dim c As Range
For Each c In Selection
If IsNumeric(c) Then
c.Value = "z" & c
End If
Next
End Sub


2017-10-16 12:21:20

Roy B

To Louis:

It rather looks like Excel recognizes the "e" as such and the accented "e" variations as an ever so slightly different "e" and sorts them after the "e", but does NOT differentiate between them. (Differentiates between a plain vowel and its accented versions, so the plain vowel sorts first, but then the accented versions remain in the order they first had as they don't sort between each other.) To put it in terms of the CODE() function, it is as if Excel sees the "e" as 101 and ALL the accented variations as 101.5.

Could be a good suggestion to make at Excel.UserVoice.com.

To Alex:

I cannot make your report happen if the column I begin with is formatted as text. I get the desired 1, 10, 101, 1a, 1b, 2, 20. Unless... when Excel asks about sorting anything that looks like a number as a number, if I choose that option, I get your results, but if I choose the other option, I get the desired result.


2017-10-16 05:41:47

DaveS

There seems to be a way of doing this without having to change the format of either the part number or helper columns (leave both as General). If part numbers are in column A and helper column is B:

B1 = TEXT(A1,"#") etc.

Select column B, then Custom Sort. On the Sort Warning dialog, select 'Expand the selection'.

On the Sort dialog, Column Sort by = B, Sort on = Values, Order = A to Z. Press <OK>.

On the Sort Warning dialog, select Sort numbers and numbers stored as text separately.


2017-10-16 04:49:56

Tim W

I use "Text to Columns" (Data tab/Excel 2010) to the same thing.


2017-10-15 06:47:38

Alex B

Louis, this is going a bit off topic but I expect to get the result you want you need to change your Excel Language options. Under Excel > Options > Choose Editing Language it says
"Add additional languages to edit your documents. The editing languages set language-specific features, including dictionaries, grammar checking, and sorting"


2017-10-15 04:20:51

Louis LAFRUIT

Many languages use accentuated characters. I expect a sort result where the order would respect the left to right order. A short (french) example shows this is not the case. The first column shows the actual result, the second column shows the order I expect.
Benne Benne
Bénne Benne/tonneau
Bènne Bénne
Benne/tonneau Bènne
Beton Beton
Béton Béton
Bétonnière Bétonnière
How can this result be obtained. It certainly is of international intrest.


2017-10-14 07:37:57

Alex B

I did not find that the proposed solution worked.
I tested it on Excel 2010 and 2016 and step 5 (Use Paste Special to paste only the values to the new column), will not make the numbers text. Although the values all left align and appear to be text an istext() formula will prove that they are not.

This of course means that when you sort the column, the numbers they will still sort as numbers.
(My sample sorted as 1, 2, 10, 20, 101, 1a, 1b etc)

My proposed solution was to apply a text to column conversion on column A (the part number), applying any delimiter that is not used ie TAB and select the output format as Text. You will then still need to format the whole column as Text since only the converted data will have applied the Text formatting.

Manual data entry will apply the columns Text characteristic and Paste Value of numbers that are already text will carry across as text, but Paste Value of a number not already text will still come across as a number in the new Text column.

The MACRO also doesn’t work, you need to modify the code to something like this c.Value = Trim(c.Value).
As it stands it also only makes it appear to be text since it left aligns but istext() and a sort will show that it is not.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.