Explanation
In this example, the XOR function contains two expressions, one to test for an “x” in column C, and one to test for an “x” in column D.
C5="x" // TRUE if coffee is "x"
D5="x" // TRUE if tea is "x"
With two logical criteria, XOR has a particular behavior, summarized in the table below:
| Coffee | Tea | Result |
|---|---|---|
| TRUE | FALSE | TRUE |
| FALSE | TRUE | TRUE |
| TRUE | TRUE | FALSE |
| FALSE | FALSE | FALSE |
At each row in column E, XOR evaluates values in columns C and D and returns a TRUE or FALSE result. This behavior is sometimes referred to as “exclusive OR”, meaning only one result can be true.
Note: with more than two criteria, XOR behavior changes, as explained on the XOR function page .
Explanation
In this example, the goal is to pad a number with zeros. To illustrate how Excel functions can be combined, the number of zeros to use is variable and comes from column C. The formula used to solve this problem combines the TEXT function and the REPT function .
Fixed number
The TEXT function returns a number formatted as text, using the number format provided. The TEXT function can apply number formats of any kind to numbers. It is most often used when you want to maintain number formatting for a number when concatenating that number with other text. For example to use the TEXT function to pad a number 3, 4, and 5 zeros:
=TEXT(17,"000") // returns "017"
=TEXT(17,"0000") // returns "0017"
=TEXT(17,"00000") // returns "00017"
Notice the number format works by adding zeros to the left as needed to reach the total number of zeros supplied.
Variable number
To allow a variable number of zeros based on a number in another cell, we can add the REPT function into the mix. The REPT function simply repeats a text string a given number of times:
=REPT("a",2) // returns "aa"
=REPT("a",3) // returns "aaa"
=REPT("a",4) // returns "aaaa"
So to pad a number with 5 zeros, we can use REPT to assemble the five zeros into the text"00000":
=TEXT(17,REPT("0",5)) // returns "00017"
The formula used in the example shown is simply a variation of the above formula. The formula in E9 evaluates like this:
=TEXT(B9,REPT("0",C9))
=TEXT(29,REPT("0",5))
=TEXT(29,"00000")
="00029"
Other characters
You can adapt this formula to use any character you like:
=TEXT(29,REPT("*",5)) // returns "***29"
=TEXT(29,REPT("-",5)) // returns "---29"
Pad for display only
Padding a number with zeros with the TEXT function changes the number into text, which may not suit your needs. To simply display a number with padding, you can use a regular number format . For example, to pad a number with 5 zeros for display only, select the cells and use the shortcut Control + 1 to open the Format Cells window. Then:
Format cells > Number > Custom > "00000"
With this approach, the number is not converted to text but remains a true number. More details here .