Using Credit Card Numbers in Excel

Posted on April 7th, 2009 in Excel Howtos , Learn Excel - 4 comments

James asks in Custom Cell Formatting in Excel Post,

Using Credit Card Numbers in ExcelI have a problem when formatting a cell that has 16 or more digits … We are working with Credit Card numbers and want to be able to enter them as a 16 digit number like this: 1234123412341234 but want them to look like this: 1234 1234 1234 1234

The problem we’re having is that the last digit always turns into a 0 (zero) and the number ends up looking like this: 1234 1234 1234 1230 The custom format type we created is: 0000 0000 0000 0000 but we’ve also tried: #### #### #### #### with the same results.
This only happens when using 16 or more digits. If we use 15 or less digits, the Custom format works fine. Unfortunately, credit cards have 16 digits so we’re stuck with the problem.
We’ve tried this on Excel 2007 with service pack 1 and on Excel 2000 with service pack 3 with the same results. The Operating Systems we’ve tried it on are Windows XP with Service Pack 3 (and all other Microsoft Updates installed) and on Windows 2000 with Service Pack 4.

Well, this is because Excel stores numbers with precision of 15 digits. So anything more and it is automatically converted in to engineering format.

Here is a simple workaround if you need to store and work with credit card numbers in excel:

Using Custom Formats in Excel to Store Credit Card NumbersChange the cell format to TEXT from general or number. Thus excel treats numbers as text and you can escape the truncation.

But what if you need to format the credit card numbers in a special way? well, we use a knife and cut the number in to 4 small parts. Not really, we use the LEFT, MID and RIGHT formulas to process the credit card number and format it the way we want.

For eg. if you want to format the credit card number 1234123412341234 like 1234 1234 1234 1234, you can use the formula

=LEFT(B3,4)&" "&MID(B3,5,4)&" "&MID(B3,9,4)&" "&RIGHT(B3,4)

and if you want to test the credit card, you know where to go. To our Excel formula E-book page.

Also: Formatting phone numbers, SSN in Excel.

Credit card Photo from Normann Copenhagen

4 Responses to “Using Credit Card Numbers in Excel”

  1. Jeff says:

    Parsing so that it can do 16 digits is great. Now, add in extra script to validate the digits (check that the first number is a 4 or 5 - Visa vs Mastercard, 6 for Discover). 🙂

  2. Kirby says:

    I put the validation in another box, and point it towards the cell with the card data.
    =IF(LEFT(D3,1)="4","Visa",IF(LEFT(D3,1)="5","Mastercard", IF(LEFT(D3, 1)="6", "Discover")))

  3. vijay ranwa says:

    Sir
    i want A1 cell input Pan No. so only First 5 text and mid 4 numberand last one text only enter
    otherwise envaild

Leave a Reply