Add leading zero to a number
This tutorial shows how to add a leading zero (0) to a number through the use of an Excel formula, with the & sign, CONCATENATE and TEXT functions or VBA
="0"&B5
=C5&B5
|
GENERIC FORMULA
=0&number
ARGUMENTS GENERIC FORMULA
=zero&number
ARGUMENTS EXPLANATION This formula uses the & sign to insert a leading zero to a number.
Click on either the Hard Coded or Cell Reference button to view the formula that either has the number zero (0) entered directly in the formula or referenced to a cell.
In this example we are adding a zero in front of a number captured in cell B5. |
=CONCATENATE(0,B5)
=CONCATENATE(C5,B5)
|
GENERIC FORMULA
=CONCATENATE(0,number)
ARGUMENTS GENERIC FORMULA
=CONCATENATE(zero,number)
ARGUMENTS EXPLANATION This formula uses the CONCATENATE function to insert a leading zero to a number
Click on either the Hard Coded or Cell Reference button to view the formula that either has the number zero (0) entered directly in the formula or referenced to a cell.
In this example we are concatenating a zero in front of a number captured in cell B5 with the use of a CONCATENATE function. |
=TEXT(B5,"000000")
=TEXT(B5,C5)
|
GENERIC FORMULA
=TEXT(number,"000000")
ARGUMENTS GENERIC FORMULA
=TEXT(number,zero)
ARGUMENTS EXPLANATION This formula uses the TEXT function to insert a leading zero to a number. It will add leading zeros to ensure the number of of a certain length, which in this example is a length of size numbers. Given the number in cell B5 has five numbers this formula will add a single value of zero (0) to the front of the number. If the number in cell B5 only comprised four numbers, this formula would add two zeros to the front of the number.
Click on either the Hard Coded or Cell Reference button to view the formula that either has the zeros entered directly in the formula or referenced to a cell. |
Dim ws As Worksheet
ws.Range("C5") = "'0" & ws.Range("B5")
End Sub
Dim ws As Worksheet
ws.Range("D5") = "'" & ws.Range("C5") & ws.Range("B5")
End Sub
Dim ws As Worksheet
'add a leading zero to a number
ws.Range("C" & x) = "'0" & ws.Range("B" & x)
Next x
End Sub
Dim ws As Worksheet
'add a leading zero to a number
ws.Range("D" & x) = "'" & ws.Range("C5") & ws.Range("B" & x)
Next x
End Sub
Dim ws As Worksheet
'add a leading zero to a number if the number comprises five numbers as we are converting the cell format to "000000"
ws.Range("C5").NumberFormat = "000000"
ws.Range("C5") = ws.Range("B5")
End Sub
Dim ws As Worksheet
'add a leading zero to a number if the number comprises five numbers as we are converting the cell format to "000000"
ws.Range("D5").NumberFormat = ws.Range("C5")
ws.Range("D5") = ws.Range("B5")
End Sub
Dim ws As Worksheet
'add a leading zero to a number if the number comprises five numbers as we are converting the cell format to "000000"
For x = 5 To 8
ws.Rangews.Range("C" & x).NumberFormat = "000000"
ws.Range("C" & x) = ws.Range("B" & x)
Next x
End Sub
Dim ws As Worksheet
'add a leading zero to a number if the number comprises five numbers as we are converting the cell format to "000000"
For x = 5 To 8
ws.Range("D" & x).NumberFormat = ws.Range("C5")
ws.Range("D" & x) = ws.Range("B" & x)
Next x
End Sub
Related Topic | Description | Related Topic and Description |
---|---|---|
Remove leading zeros | How to remove the leading zeros in a string through the use of an Excel formula or VBA | |
Remove leading spaces in a cell | How to remove only the leading spaces from text in a cell through the use of an Excel formula or VBA |
Related Functions | Description | Related Functions and Description |
---|---|---|
TEXT Function | The Excel TEXT function returns a numeric value as text, in a specified format |