14 digit Check Digit Calculation in Luhn algorithm

zhosen

New Member
Joined
Apr 4, 2013
Messages
4
Hi,
I am new comer here. I need a favor to make a formula in Excel to create series of number for 14 Digit Check Digit. Here is my requirements-
I have 14 Digit of data [IMEI number] and i want to make the 15th digit as check digit as per Luhn algorithm. For example-

IMEI: 35145120840121
(5x2, 4x2, 1x2, 0x2, 4x2, 1x2, 1x2) = (10, 8, 2, 0, 8, 2, 2)
(1+0+8+2+0+8+2+2) + (3+1+5+2+8+0+2 ) = 44
Luhn Digit : 6

And final digit will be: 351451208401216
So, how to make the formula in excel? Please help me to find it out. I will appreciate if anybody can help me. thanks in advance. Have a great day!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Alt+F11
Click Insert => Module
Paste the following code:

Code:
Option Explicit

Function AddLuhnDigit(target As Range) As String
Dim sTmp As String
Dim i As Integer, iTmp As Integer

For i = 2 To Len(target.Text) Step 2
    sTmp = sTmp & (CInt(Mid(target.Text, i, 1)) * 2)
Next i

For i = 1 To Len(sTmp)
    iTmp = iTmp + CInt(Mid(sTmp, i, 1))
Next i

For i = 1 To Len(target.Text) Step 2
    iTmp = iTmp + CInt(Mid(target.Text, i, 1))
Next i

AddLuhnDigit = target.Text & (10 - (iTmp Mod 10))

End Function

You can then use it on your sheet like any other formula, so assuming your list of numbers is in column A, then put =AddLuhnDigit(A1) in B1 and copy down.

Let me know if it works.
 
Upvote 0
Thank you nuked. Your code is great. I tried it and it works but it displays 10 instead of 0 if the check digit is 0. Please update the code so as if the check digit is 0, the code to dialysis 0 not 10. Are you able to do so?

Thank you in advance and warm regards.
 
Upvote 0
Hi
Try:

Rich (BB code):
 AddLuhnDigit = target.Text & Iif((10 - (iTmp Mod 10))=10,"0",(10 - (iTmp Mod 10)))
 
Last edited by a moderator:
Upvote 0
Hi nuked,
Now it works. Thank you very much. It will help me a lot at my school excel project. Yo are a master :)

Regards
 
Upvote 0
please help!!!! first code worked perfectly got the same issue of displaying 10 instead of 0 if the check digit is 0. I tried the code under below but receive a compile error
 
Upvote 0
Golly I did this so many years ago. @coder4learning please start your own thread with your question and refer back to this thread if you need to.
 
Upvote 0

Forum statistics

Threads
1,223,104
Messages
6,170,125
Members
452,303
Latest member
c4cstore

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