Hello. I might be wrong, but it looks like a credit card check multiplies
each digit by an alternating series of 1's or 2's. You then Sum the
individual digits. If the Total divides by 10, the number is valid. If
this is correct, here is one idea. If you wanted, you can check for leading
characters that are valid for MasterCard, Visa, etc. I used a string
variable since entering a 16 digit number would have to be done as a string.
My credit card showed "Valid" as a check.
I'm not an expert here, so hopefully some ideas here will help.
Function CC_Check(n As String) As Boolean
'// Dana DeLouis
Dim P As Long 'Pointer
Dim Tot As Long
Dim digit As Long
Dim s As Long
Select Case Len(n)
Case 13 To 16
'Valid Length
'// Make Even Length
If Len(n) Mod 2 = 1 Then n = "0" & n
For P = Len(n) To 1 Step -1
'// = = = = = = = = =
'Option #1
'// = = = = = = = = =
' Get Character
s = Mid$(n, P, 1)
' Multiply by 1 or 2
digit = s * (1 + (P Mod 2))
' Sum the individual digits (ie 12 = 1+2=3)
Tot = Tot + ((digit - 1) Mod 9) + 1
'Option #2 -> Combines the 3 lines above...
'// = = = = = = = = =
' Tot = Tot + ((Mid$(n, P, 1) * (1 + (P Mod 2)) - 1) Mod 9) + 1
'// = = = = = = = = =
Next P
CC_Check = (Tot Mod 10) = 0
Case Else
'Invalid
CC_Check = "Invalid Length"
End Select
End Function