Sunday, July 16, 2017

Keep leading zeros

Keep leading zeros

Do you ever use things like social security numbers, phone numbers, credit card numbers, product codes, or postal codes in your workbooks? These are all examples of number codes—things that look like numbers, but that aren't intended to be used in formulas.

When you import or paste numbers codes like these into your workbook, Excel interprets them as numbers and applies a general or number format to them. As a consequence, any leading zeros are removed from the number codes, which is often not what you want. In this article, you'll learn how to retain leading zeros in number codes.

In addition, if you are using credit card numbers, or other number codes that contain 16 digits or more, you must use a text format instead of a number format. That's because Excel has a maximum of 15 digits of precision and will round any numbers that follow the 15th digit down to zero.

In this article

Why Excel removes leading zeros

Use a custom format to keep the leading zeros

Why Excel removes leading zeros

Number codes get into your Excel workbook in many ways. You might simply type them in, or copy and paste them from another program. Or, you might open a text file, or import data from a data source, such as an Access database. In many cases, Excel converts these number codes to a general or a number format. The default behavior of those formats is to remove any leading zeros and, depending on the length of the number, use scientific notation. Excel treats a number code as just another number, but you know that it's a number code, and that the leading zeros need to stay put.

Number
code

Fictitious example

Default behaviors
(varies with cell width)

Social
security

012345678

12345678
1.2E+07

Phone

0014255550177

14255550177
1.4E+10

Postal
code

00123

123

You might not even see this happen if you are dealing with a long list of number codes. So it's important to be aware of the issue, especially when the data is used in databases or other programs. Although you can convert the number format to a text format, you may not want to convert numbers to text, especially if you have a large list of long numbers, because this can increase the size of your workbook.

Use a custom format to keep the leading zeros

If you want to resolve the issue just within the workbook because it's not used by other programs as a data source, you can use a custom or a special format to keep the leading zeros. This works for number codes that contain fewer than 16 digits,

In addition, you can separate some of the digits in your number codes with dashes by adding these dashes to the custom format. For example, to make a phone number more readable, you can add a dash between the international code, the country/region code, the area code, the prefix, and the last few numbers.

Number
code

Fictitious example

Number format and
new behavior

Social
security

012345678

000-00-0000 
012-34-5678 

Phone

0012345556789

00-0-000-000-0000 
00-1-234-555-6789 

Postal
code

00123

00000 
00123 

Procedure    

  1. Select the cell or range of cells that you want to format.

    How to select a cell or a range

    To select

    Do this

    A single cell

    Click the cell, or press the arrow keys to move to the cell.

    A range of cells

    Click the first cell in the range, and then drag to the last cell, or hold down SHIFT while you press the arrow keys to extend the selection.

    You can also select the first cell in the range, and then press F8 to extend the selection by using the arrow keys. To stop extending the selection, press F8 again.

    A large range of cells

    Click the first cell in the range, and then hold down SHIFT while you click the last cell in the range. You can scroll to make the last cell visible.

    All cells on a worksheet

    Click the Select All button.

    Select All button

    To select the entire worksheet, you can also press CTRL+A.

    Note: If the worksheet contains data, CTRL+A selects the current region. Pressing CTRL+A a second time selects the entire worksheet.

    Nonadjacent cells or cell ranges

    Select the first cell or range of cells, and then hold down CTRL while you select the other cells or ranges.

    You can also select the first cell or range of cells, and then press SHIFT+F8 to add another nonadjacent cell or range to the selection. To stop adding cells or ranges to the selection, press SHIFT+F8 again.

    Note: You cannot cancel the selection of a cell or range of cells in a nonadjacent selection without canceling the entire selection.

    An entire row or column

    Click the row or column heading.

    Worksheet headings

    1. Row heading

    2. Column heading

    You can also select cells in a row or column by selecting the first cell and then pressing CTRL+SHIFT+ARROW key (RIGHT ARROW or LEFT ARROW for rows, UP ARROW or DOWN ARROW for columns).

    Note: If the row or column contains data, CTRL+SHIFT+ARROW key selects the row or column to the last used cell. Pressing CTRL+SHIFT+ARROW key a second time selects the entire row or column.

    Adjacent rows or columns

    Drag across the row or column headings. Or select the first row or column; then hold down SHIFT while you select the last row or column.

    Nonadjacent rows or columns

    Click the column or row heading of the first row or column in your selection; then hold down CTRL while you click the column or row headings of other rows or columns that you want to add to the selection.

    The first or last cell in a row or column

    Select a cell in the row or column, and then press CTRL+ARROW key (RIGHT ARROW or LEFT ARROW for rows, UP ARROW or DOWN ARROW for columns).

    The first or last cell on a worksheet or in a Microsoft Office Excel table

    Press CTRL+HOME to select the first cell on the worksheet or in an Excel list.

    Press CTRL+END to select the last cell on the worksheet or in an Excel list that contains data or formatting.

    Cells to the last used cell on the worksheet (lower-right corner)

    Select the first cell, and then press CTRL+SHIFT+END to extend the selection of cells to the last used cell on the worksheet (lower-right corner).

    Cells to the beginning of the worksheet

    Select the first cell, and then press CTRL+SHIFT+HOME to extend the selection of cells to the beginning of the worksheet.

    More or fewer cells than the active selection

    Hold down SHIFT while you click the last cell that you want to include in the new selection. The rectangular range between the active cell and the cell that you click becomes the new selection.

    Tip: To cancel a selection of cells, click any cell on the worksheet.

  2. On the Home tab, in the Number group, click the dialog box launcher Button image next to Number.

    Dialog Box Launcher in Number group

  3. In the Category list, click Custom and then, in the Type box, type the number format, such as 000-00-0000 for a social security number code, or 00000 for a five-digit postal code.

    Tip: You can also click Special, and then select Zip Code, Zip Code + 4, Phone number, or Social Security Number.

    Find more information about custom codes, see Create or delete a custom number format.

Leading zeros and other programs

If you are using another program that opens your workbook as a data source, you should see how that program handles leading zeros.

For example, suppose you want to use a list of addresses in a workbook as the data source for a Mail Merge operation, and one of the columns is a zip code with a custom format of 00000. In this scenario, Access keeps the leading zeros, but Word removes the leading zeros. In the case of Word, you can specify a calculated column as the postal code field in the Mail Merge operation to ensure that the leading zeros are not removed.

Number
code

Fictitious example
(In cell A1)

TEXT function and
new behavior

Social
security

012345678

=TEXT(A1,"000-00-0000")
012-34-5678

Phone

0012345556789

=TEXT(A1,"00-0-000-000-0000")
00-1-234-555-6789

Postal
code

00123

=TEXT(A1,"00000")
00123

For more information about the TEXT function, see TEXT function.

Credit card number codes are rounded down

To further complicate the matter, Excel has a maximum precision of 15 significant digits, which means that for any number containing 16 or more digits, such as a credit card number, the last digit is rounded down to zero.

Number
code

Fictitious example

Default behaviors
(varies with cell width)

Credit
card

0123456789012345

1.23457E+14

Even if you use a custom number format, the number is still rounded down to zero when there is no leading zero.

Number
code

Fictitious example

Number format and
new behavior

Credit
card
(With
leading
zero)

0123456789012345

0000-0000-0000-0000
0123-4567-8901-2345

Credit
card
(Without
leading
zero)

1234567890123456

0000-0000-0000-0000
1234-5678-9012-3450

In the case of number codes that are 16 digits or larger, you must use a text format. To do this, you can do one of two things:

Use the apostrophe character

For a small set of number codes, you can type an apostrophe character (') in front of the number code and then add back the leading zeros.

Convert the number to text when you import text data

In Step 3 of the Text Import Wizard (On the Data tab, in the Get External Data group, click From Text), you can select the column of data that contains the credit card number, and then explicitly choose a Text column data format.

Top of Page

1 comment:

  1. Microsoft Office Tutorials: Keep Leading Zeros >>>>> Download Now

    >>>>> Download Full

    Microsoft Office Tutorials: Keep Leading Zeros >>>>> Download LINK

    >>>>> Download Now

    Microsoft Office Tutorials: Keep Leading Zeros >>>>> Download Full

    >>>>> Download LINK gc

    ReplyDelete