Luhn check digit formula

bhmcpfs

Board Regular
Joined
Apr 1, 2002
Messages
106
I'm charged with coming up with a formula using the "Luhn check digit formula" for a 10-digit number, calculated as follows: (The check digit is the last number.)

1. Double the value of alternate digits beginning with the first digit.
2. Add 24 to the individual digits of the products resulting from step 1 to the unaffected digits from the original number.
3. Subtract the total obtained in step 2 from the next higher number ending in zero. This is the check digit. If the total obtained in step 2 is a number ending in zero, the check digit is zero.

Example: Number is 1234567893

1 2 3 4 5 6 7 8 9 3
2 6 10 14 18 (step 1)
24+2+2+6+4+1+0+6+1+4+8+1+8=67 (step 2)
70-67=3 (step 3)

I'm not getting anywhere with my attempts, and the formula is getting uglyier (is that a word?) with each attempt. Any help from the board would be much appreciated! Thanks in advance, and sorry for the long post...
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Thanks. I tried various luhn check digit searches and didn't have any luck...

Seems like the long formulas add a check digit to my #'s. But my numbers already have the check digit added - I need to verify their accuracy. I've been trying to use something involving sum/len and am stuck at step 2 (didn't get very far, did I?)
 
Upvote 0
Hi
Insert your test number in cell A1 and Paste the following codes in the macro window (Alt F8)

Code:
For a = 1 To Len(Cells(1, 1)) Step 2
Cells(a + 1, 2) = Mid(Cells(1, 1), a, 1) * 2
Cells(a + 2, 2) = Mid(Cells(1, 1), a + 1, 1)
Next a
Cells(1, 3) = 24
For b = 2 To Len(Cells(1, 1))
If Len(Cells(b, 2)) > 1 Then
C = Mid(Cells(b, 2), 1, 1)
d = Mid(Cells(b, 2), 2, 1)
Cells(1, 3) = Cells(1, 3) + C + d
Else
Cells(1, 3) = Cells(1, 3) + Cells(b, 2)
End If
Next b
Cells(2, 3) = "=round(C1,-1)"
Cells(3, 3) = "=C2-C1"
run the macro. Cell C3 gives answer as 3.
Ravi
 
Upvote 0
forgot to add that I need a formula, some workplaces won't allow macros. Very sorry I didn't post this originally...
 
Upvote 0
Hi

This is the formula I was thinking of:

Excel Workbook
AB
1Credit Card NumberLuhn Check
2633450509813425051
3123456789750
4894563245145
Sheet3
 
Upvote 0
Wow!... formula is WAY more involved than I can figure out. But way shorter than mine...

Formula seems to verify a 2-digit check number, correct? Mine needs to validate a single digit...

Two samples:

1104806504
1356310189

check digits are 4 and 9 respectively (the 10th digit). How should your formula be modified to validate?
 
Upvote 0
Hi

The following illustrates the calculation steps. It doesn't need to be modified - it will work with even numbered card numbers. You will probably want the card numbers enetered as text in the cell though (ie '123456 etc rather than 1234567 etc - note the single quote).

Excel Workbook
AB
1Credit Card NumberLuhn Check
2123456789750
3#=SUM(--MID(TEXT(MID(A2,{1;2;3;4;5;6;7;8;9;10},1)*(MOD({1;2;3;4;5;6;7;8;9;10},2)+1),"00"),{1,2},1))
4#=SUM(--MID(TEXT({"1";"2";"3";"4";"5";"6";"7";"8";"9";"7"}*({1;0;1;0;1;0;1;0;1;0}+1),"00"),{1,2},1))
5#=SUM(--MID(TEXT({"1";"2";"3";"4";"5";"6";"7";"8";"9";"7"}*{2;1;2;1;2;1;2;1;2;1},"00"),{1,2},1))
6#=SUM(--MID({"02";"02";"06";"04";"10";"06";"14";"08";"18";"07"},{1,2},1))
7#=SUM(--{"0","2";"0","2";"0","6";"0","4";"1","0";"0","6";"1","4";"0","8";"1","8";"0","7"})
8#=SUM({0,2;0,2;0,6;0,4;1,0;0,6;1,4;0,8;1,8;0,7})
950
Sheet3
 
Upvote 0
Yeah, I noticed that too (that the two examples fail). I think I see my error... The last digit is the check digit, and should not be used in the calculation. So 1104806504 should yield 22, but I have to add a constant 24, which gives 46. 50-46=4. Check. 1356310189 should yield 27, plus 24 = 51. 60-51=9. Check again. How should your formula be altered to fix? Thanks a bunch for all your help!
 
Upvote 0

Forum statistics

Threads
1,223,107
Messages
6,170,137
Members
452,304
Latest member
Thelingly95

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top