Michael Frank figured out what's going on. A custom format of "0000" adds leading zeros as necessary to display four digits, but it doesn't alter what is in the cell. If you apply that format to 102
, the cell will display 0102
, but if you use LEN() to test the number of characters, that will show three. You can use this for a solution with a short formula.
In A1, I've got the value 102
. In A2 is the same value, custom formatted 0000
. The formula in B2 is:
=LEFT(REPT("0",4-LEN(A2))&A2,2)
This subtracts the actual length of the value in A2 from 4, then creates the resulting count of leading zeros and concatenates that to the actual value in A2. Then it takes the left two characters of the result.
Row 3 shows the result when the value in column A is actually four characters.