# Sorting Text as Numbers

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

Kyle has a worksheet that consists of three columns of data: Part Number, Quantity, and Length. Length is designated in inches, with a quotation mark to show it is in inches (such as 30", 54", or 100"). Kyle needs to sort the data from shortest to longest length, but Excel sorts the lengths as text, such that 100" comes before 30". He wonders if there is a way to get Excel to sort the textual information as if it were numbers, so 30" correctly comes before 100", without getting rid of the quotation marks.

The short answer is that you can't do it, at least not directly. When you include the quote mark in the cell, Excel treats the entire cell as text and sorts it as such. And, as Kyle noted, the text string 100" comes before 30" because 1 comes before 3 in a textual sort.

There are things you can do, however. For instance, if you use a formula to generate the values in your Length column, you could modify the formula so that it "front pads" the lengths with zeroes, as necessary. Using this approach you would not have lengths like 30", 54", or 100", but instead you would have 030", 054", and 100". As long as all the lengths used the same number of digits, the sorting will occur correctly.

You could also add a helper column to the right of the Length column and, in the helper column, place the numeric values of whatever is in the cell to the left. So, if your first data in in cell C2, in cell D2 (your helper column) you could enter the following:

```=VALUE(SUBSTITUTE(C2,"""",""))
```

The four quote marks are necessary as the second parameter of the SUBSTITUTE function in order to get rid of quote marks in C2. The result is that D2 contains the numeric value of whatever was in cell C2. Copy the formula down as far as necessary, and then use column D to do the sorting. After sorting you can even hide column D, if desired, or make it as narrow as you want.

Another approach that can work well if you have a limited number of lengths is to create a custom list and then use that list to do the sorting. Here's how to set up the custom list for your lengths:

1. Display the Excel Options dialog box. (In Excel 2007 click the Office button and then click Excel Options. In Excel 2010 or later versions, display the File tab of the ribbon and then click Options.)
2. If you are using Excel 2007, make sure Popular is selected at the left of the dialog box. If you are using Excel 2010 or a later version, click Advanced and then scroll toward the end of the options until you see the General area.
3. Click Edit Custom Lists. Excel displays the Custom Lists dialog box and hides the Excel Options dialog box. (See Figure 1.)
4. Figure 1. The Custom Lists dialog box.

5. Select NEW LIST in the Custom Lists list.
6. In the List Entries portion of the dialog box, start typing the items in your list, in the order they should appear. For instance, if you have only 15 possible lengths, type all the lengths, in their proper order, makng sure to include the quote mark after each length. Press Enter at the end of each length you add.
7. When you are done, click the Add button.
8. Click OK to close the Custom Lists dialog box. The Excel Options dialog box reappears.
9. Click OK to close the Excel Options dialog box.

Once the custom list is defined, you can use the Sort dialog box and specify that you want to do a custom sort. Pick your new list and that is what Excel will use when arranging the rows in the worksheet. Again, this approach only really works if you have a limited number of lengths and you know ahead of time what those lengths will be.

Perhaps the best solution, though, is to remove the quotation marks. (Yes, I know... Kyle said he didn't want to get rid of them, but bear with me for a moment.) If everything in the Length column is shown in inches, then you can get rid of the explicit quote marks and create a custom format that will display them. Getting rid of the quote marks is easy—just use Find and Replace to remove them. (Search for a quote mark and replace it with nothing.) Then, create the custom format in this manner:

1. Select all the cells that contain lengths. (You can select the entire column, if desired.)
2. Display the Home tab of the ribbon.
3. Click the small icon at the lower-right corner of the Number group. Excel displays the Format Cells dialog box, with the Number tab selected.
4. In the Category list, choose Custom. The dialog box changes so you can enter a custom format. (See Figure 2.)
5. Figure 2. The Number tab of the Format Cells dialog box.

6. In the Type box, enter the following: 0.0\"
7. Click OK.

You should now see all the cells (selected in step 1) displayed with the trailing quote mark. It is the format specified in step 5 that instructs Excel to include the quote mark after any number. It also instructs Excel to display one digit after the decimal point. (You can modify the format so it displays a different number of digits, if desired. If you just want the number to be generally visible, then use this custom format:

```#\"
```

As with the custom format in step 5 of the steps just shown, the backslash is necessary so that Excel knows the quote mark is part of the custom format itself.

Again, the custom format route works great if everything in the Length column is shown in inches. If it isn't all in inches, then you could move the dimension indicator (quote mark, apostrophe, whatever) to column D and remove it from column C. Make column D just as narrow as necessary to display the dimension indicator, and then sort by column C.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3917) 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

Spell Checking Forms

Word may be used to create protected forms that limit where the user may input data. Normally spell checking is disabled ...

Discover More

Missing PivotTable Data

Wonder what happened to the data behind a PivotTable? It could be in a number of places, and tracking it down could be a ...

Discover More

Rounding to Even and Odd Values

Want to round values so they are always even or odd? You can do it quickly and easily by using the EVEN and ODD worksheet ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

##### More ExcelTips (ribbon)

Importing Custom Lists

Custom lists are handy ways to enter recurring data in a worksheet. Here's how you can import your own custom lists from ...

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

Storing Sorting Criteria

Need to do the same sorting operation over and over again? Excel doesn't provide a way to save your sorting criteria, but ...

Discover More
##### Subscribe

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

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 8 - 3?

2021-06-24 04:13:04

Willy Vanhaelen

@Mikel
This is even shorter: =LEFT(D4,LEN(D4)-1)*1

2021-06-23 08:50:46

MikeL

This seems to work and is shorter - in a helper column: =VALUE(LEFT(D4,LEN(D4)-1))

2017-08-07 04:32:22

David Robinson

Guy,
To embellish Rod's explanation on the four double quotes, the meaning of each quote is:
first one = opens string
second one = an ESCAPE CHARACTER to tell Excel to treat the next character as it is, and not to treat it as closing the string
third one = the actual character to substitute
fourth one = closes string.

Hope this helps. If you're unclear on escape characters they're worth a quick read, especially as my preferred option, the format mask, uses the \ escape character.

2017-08-06 02:38:39

Aldo

Use a number format to display inches like . . .

# ##/##\";[Red]- # ##/##\";0\"

This will display whole numbers and fractions properly in inch format.

2017-08-05 09:06:07

Sheryl Lucas

If he changed the column label to "length (in inches)" or "length (in.)" it would eliminate the need for the quote mark altogether.

2017-08-05 08:54:19

Allen

Guy: I would, too, think it would make sense to use only three quote marks. (Or, better still, a quote followed by a backslash, and then two more quotes.) As Rod notes, though, it needs to be four quote marks.

-Allen

2017-08-05 06:34:39

Rod Grealish

The quote (") is being used for two purposes, as a string delimiter and as a literal character. In this case the literal quote is doubled ("") to show that it is not to be interpreted as closing the string. This is a common problem in many programming languages where a character has a special usage in the language and also needs to be used literally without its special usage. The problem arises in wildcard searches in Word where some characters have special usages such as [,{,(. If you want to match one of these characters in a search then you need to precede it by a \ - which also means that to match a \ you need to write it as \\.

2017-08-05 05:33:59

Guy

Hello Allen:

Thanks for all the great ideas you share. In this formula, =VALUE(SUBSTITUTE(C2,"""","")), shouldn't there just be 3 quotes in the instead of 4 since 30" only has 1 quotation mark?

##### 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.