

The macro actually changes the cell contents-no longer will the cells contain numeric values (the cause of the original problem), but they will contain text values. The number of zeros in the first section is equal to the length of your field, including the leading zeros. Then, enter this calculation: Right (000000 + Str ( EmployeeID),6) Where: EmployeeID is the name of your field. To use the macro, simply select the range of cells containing the ZIP Codes, then run the macro. First, create a calculated field by right-clicking anywhere in the Data window in your worksheet. This is due to the information coming from the datatable to excel. However, when I create the file in excel, my leading zeros for my primary key are missing due to being converted to numbers instead of text.

Selection.NumberFormat = Each ThisCell In Selection I am exporting a SQL table to an Excel file using the following method below. If you want the underlying data to actually include the leading zeroes, it is best to use a macro that goes through and adds leading zeroes to the information in a cell.

(Excel provides a special format for ZIP Codes, available through the Number tab of the Format Cells dialog box.) This may work for the display, but the underlying data is still missing the leading zeroes.
#Leading 0 disappears in excel switch zip to 5 digits code
One solution, of course, is to simply change the display format used for ZIP Code cells. This results in leading zeroes being dropped from the ZIP Codes, which can obviously cause problems later when using the data for its intended purpose. View the results in Tableau by downloading the attached workbook below.When you import ZIP Codes from a text file into an Excel workbook, it is not uncommon for Excel to translate the values as numbers rather than as ZIP Codes.

This is a simple and effective way to handle it, using Tableau to make your data shine! It matters not how we got here, it just matters that nearly every corporation has this problem in some form or another. I can only assume that using leading zeros is a vestige of the olden days, back when fields all needed to be a fixed-length because database tools were not as sophisticated. All you have to do is change the number of zeros and the numerical value in the ‘Right’ function. The absolute beauty of this trick is that it will work for any size field you need. Here is a side-by-side comparison of the original EmployeeID and the newly formatted one in a Tableau worksheet: Here is what that looks like in the Calculation box in Tableau: In this example, the field length we need, including leading zeros, is 6.
