Private Sub PersonnummerKoll(ByVal Personnummer As String)
'Returns true for a Correct number, else false
Dim Siffra(9) As Integer
Dim HelNummer As String
Dim Resultat As Integer
Dim StrResultat As String
Dim i As Integer
'Remove "-" if there is one
i = InStr(Personnummer, "-")
If i = 7 Then
Personnummer = Mid(Personnummer, 1, 6) & Mid(Personnummer, 8, 11)
End If
'check length
If Len(Personnummer) <> 10 Then
'Return False
MsgBox ("Fel antal tecken!")
Exit Sub
End If
'split in strings
For i = 1 To 9
Siffra(i) = CInt(Mid(Personnummer, i, 1))
Next
'double number in odd positions
For i = 0 To 9 Step 2
Siffra(i + 1) = Siffra(i + 1) * 2
Next
'add to digits strings and add strings
For i = 1 To 9
If Siffra(i) >= 10 Then Siffra(i) = Val(Mid(Siffra(i), 1, 1)) + Val(Mid(Siffra(i), 2, 1))
Resultat = Resultat + (Siffra(i))
Next
StrResultat = CStr(Resultat)
i = CInt(Mid(Personnummer, Len(Personnummer), 1))
If 10 - Val(Mid(StrResultat, Len(StrResultat), 1)) = i Then
'Return True
MsgBox ("Korrekt pnr!")
Else
'Return False
MsgBox ("Felaktigt pnr!")
End If
End Sub