How to validate a card number with the Luhn check in Excel

4111111111111111 is a test number. xl.luhn returns TRUE. Change the last digit to 2 and it returns FALSE. A pass means the digits are consistent with each other. It does not mean a card was issued, funded, or approved for a charge. Do not store a real card number in a workbook you do not need to keep.

The cell has to be text. Excel keeps 15 significant digits. A 16-digit card typed as a number is already a different number before the checksum runs. Keep the leading apostrophe, or format the cell as text, before you type the digits.

xl.luhn
=xl.luhn(B1)
=xl.luhn(B1)B2
A 16-digit test number
AB
1Card4111111111111111
2LuhnTRUE
3Changed4111111111111112
4LuhnFALSE
A picture of the Luhn page. The card is text. 4111111111111111 passes. 4111111111111112 fails. This is a checksum, not a charge.

Why the total is 30

From the right, every second digit is doubled, and the last digit is not doubled. A doubled digit over 9 drops by 9. On this test number the first digit is 4, so it doubles to 8. The other doubled digits are ones, and 1 × 2 = 2. There are seven of those. The eight digits that are not doubled stay 1.

=SUMPRODUCT({8,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1})

8 + (2 × 7) + (1 × 8) = 8 + 14 + 8 = 30. MOD of 30 and 10 is 0, so the check passes. The same MOD on the boxes example is a different remainder: MOD(17, 5) is 2.

The last digit of 4111111111111112 is not doubled. That piece is 2 instead of 1, so the total is 31. MOD(31, 10) is 1, which is not 0.

SUMPRODUCT
=SUMPRODUCT({8,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1})
=SUMPRODUCT({8,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1})B1
The sixteen contributions
AB
1Sum30
2Remainder0
A picture of the pieces after doubling. The first digit 4 becomes 8. Seven ones become 2. Eight ones stay 1. The sum is 30. MOD of 30 and 10 is 0.

SUMPRODUCT of one list adds it. The grocery line on that page is a different product: 2 × $3.49, 1 × $4.29, and 3 × $4.79 total $25.64.

Hyphens

xl.digits.only keeps the digits and drops the hyphens. 4111-1111-1111-1111 becomes the text 4111111111111111, and the Luhn check of that text is TRUE. The same name on the phone 512-555-0148 is the text 5125550148. Do not point it at money. $1,234.50 would keep 123450.

Both names need Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME?. Define xl.digits.only as well, because xl.luhn calls it.


More from the blog