For example, you may want to get a consistent look in your data set as shown below:
In this tutorial, you’ll learn various ways to add leading zeroes in Excel (i.e., add a 0 in front of a number):
Converting the Format to Text Using Custom Number Formatting Using Text Function Using REPT/LEN functions Using VBA
Each of these methods has some merits and drawbacks (covered in each section). Let’s see how each of these works.
Add Leading Zeros by Converting the Format to Text
When to Use: When you have a small numeric data set, and you plan to do this editing manually. Suppose you have employee Ids of the marketing department as shown below and you want to make these Ids look consistent by adding leading zeroes.
So you try and change the id by entering leading zeroes (00001 instead of 1). But to your amazement, Excel converts it back to 1.
This happens as Excel understands that 00001 and 1 are the same numbers and should follow the same display rules. Now as frustrating as it may be for you, Excel has its reasons. So, to get the work done without bending Excel rules, you’ll have to take advantage of the fact that this rule doesn’t apply to text formatting. So here is what you need to do:
That’s it! Now, when you enter leading zeroes manually, Excel would readily comply. Caution: When you convert the format to Text, some Excel functions will not work properly. For example, SUM/COUNT function would ignore the cell since it’s in the text format.
Add Leading Zeros by Using Custom Number Formatting
When to Use: When you have a numeric dataset, and you want the result to be numeric (not text). When you display a number in a specific format, it doesn’t change the underlying value of the number. For example, I can display the number 1000 as 1000 or 1,000 or 1000.00 or 001000 or 26-09-1902 (even dates are numbers in the backend in Excel). In all the different ways to display the number, the value of the number never changes. It’s only the way it’s displayed that is changed. To add leading zeroes, we can format it to show it that way, while the underlying value would remain unchanged. Here are the steps to use this technique to add leading zeroes in Excel: Doing this will always display all the numbers as five digits, where leading 0’s are automatically added if the number is less than 5 digits. So 10 would become 00010 and 100 would become 00100. In this case, we have used six zeroes, but if your data has numbers with more digits, then you need to use the format accordingly. Note: This technique would work only for a numeric dataset. In case you have employee ids like A1, A2, A3 and so on, then these are text and would not change when you apply the custom format as shown above.
Add Leading Zeros by Using TEXT Function
When to Use: When you want the result to be text. TEXT function allows you to change the value to the desired format. For example, if you want 1 to be displayed as 001, you can use the TEXT function for that. However, remember that the TEXT function would change the format and make it TEXT. This means that when you make 1 as 001, Excel treats the new result as text with three characters (just like abc or xyz). Here is how to add leading zeroes using the TEXT function: This will display all the numbers as five digits, where leading 0’s are automatically added if the number is less than 5 digits. One benefit of converting data into text is that you can now use it in lookup formulas such as VLOOKUP or INDEX/MATCH to fetch the details of an employee using his/her employee id. Note: This technique would work only for a numeric dataset. In case you have employee ids like A1, A2, A3 and so on, then these are text and would not change when you apply the custom format as shown above.
Add Leading Zeros by Using REPT and LEN Functions
When to Use: When you have a data set that is numeric/alphanumeric, and you want the result to be text. The drawback of using the TEXT function was that it would only work with numeric data. But in case you have an alphanumeric dataset (say A1, A2, A3, and so on), then the TEXT function would fail. In such cases, a combination of REPT and LEN function does the trick. Here is how to do it: This would make all the numbers/strings 5 characters long with leading zeroes wherever needed. Here is how this formula works:
LEN(A2) gives the length of the string/numbers in the cell. =REPT(0,5-LEN(A2)) would give the number of 0 that should be added. Here I have used 5 in the formula as that was the maximum length of string/numbers in my dataset. You can change this according to your data. =REPT(0,5-LEN(A2))&A2 would simply add the number of zeroes to the value of the cell. For example, if the value in the cell is 123, this would return 00123.
Add Leading Zeros by Using Custom Function (VBA)
If adding leading zeroes in Excel is something you are required to do quite often, using a custom function is a good idea. Here is the VBA code that will create a simple function for adding leading zeroes: Simply add this code to the module code window, and you’ll be able to use it just like any other worksheet function. Or create an add-in for it and be able to share it with your colleagues. In this tutorial, I covered five ways to add a 0 in front of numbers in Excel. These are called leading zeros and while Excel removes them by default, you can still put a 0 in front of a number by using the formatting and formula methods covered in this tutorial.
How to Remove Leading Zeros in Excel Remove Spaces in Excel – Leading, Trailing, and Double Six Awesome Things Excel Custom Number Format can Do for You. 10 Ways to Clean Data in Excel Spreadsheets. How to Lock Cells in Excel. How to Add Decimal Places in Excel (Automatically)
I have tried a wide variety of approaches and nothing works. HELP, please. The TEXT function works the best for numbers, but not for actual text, for example if I get a part number like QJE and it needs to be 00QJE, Text doesn’t work. For the QJE example, =REPT(0,5-LEN(A2))&A2 works, but in my experience sometimes the part number is longer than 5 characters, in which case the formula fails (#VALUE! error). Unusual, but possible. So the solution is to use CONCATENATE, like this: =RIGHT(CONCATENATE(“00000”,A2),5), using as many zeros as the total expected length of the part number. This provides full flexibility and it handles and corrects part numbers than are anywhere from 1 to 5 or more characters. Using “&” instead of the CONCATENATE function also works: =RIGHT(“00000″&A2,5) and makes the formula shorter. Thanks, Pablo