Mod-10 Check Digit

U

Union70

Thanks for everyone's input. I was able to create this spreadsheets.

Thanks,
John




Harlan said:
"Myrna Larson" (e-mail address removed) wrote...-
In case anyone is interested in a VBA solution, here's a routine. Th
input
can include digits plus dashes and spaces (which are ignored). If it-
includes-
any other characters, it returns -1 to indicate an error.-
....

Me, I prefer a more pointer-like approach, and I prefer to keep error
checking outside loops.


Function LUHN(ByVal ds As String, Optional dw As Boolean = False) A
Long
Const EVENDIGITS As String = "0516273849"
Const ODDDIGITS As String = "0123456789"

Dim k As Long, n As Long
Dim ed As String, od As String

ds = Application.WorksheetFunction.Substitute(ds, " ", "")
ds = Application.WorksheetFunction.Substitute(ds, "-", "")

If ds Like "*[!0-9]*" Then
LUHN = -1
Exit Function
End If

n = Len(ds)
LUHN = -n

If dw Then
ed = EVENDIGITS
od = ODDDIGITS
Else
ed = ODDDIGITS
od = EVENDIGITS
End If

For k = n To 2 Step -2
LUHN = LUHN + InStr(od, Mid(ds, k, 1)) + InStr(ed, Mid(ds, k - 1,
1))
Next k

If k = 1 Then LUHN = LUHN + InStr(od, Mid(ds, k, 1))

LUHN = (10 - LUHN Mod 10) Mod 10
End Function[/QUOT
 
M

Myrna Larson

Me, I prefer a more pointer-like approach, and I prefer to keep error
I'd call this a translation table rather than pointers. For the digits that
are not doubled, the digit N is translated to N + 1. For digits to be doubled,
you translate N to N * 2 + 1. In both cases, you subtract 9 if the result is >
9.

Interestingly enough, the calls to Substitute and the Like function are so
slow that with this particular problem, it's actually faster to do the error
checking in-line. And that's in spite of your loop executing only half as many
times.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top