Last active
December 6, 2017 00:49
-
-
Save aklefdal/1367a06bba1728399f232e3e7340ff9d to your computer and use it in GitHub Desktop.
VBA-funksjoner for å gjøre MOD10-generering av kontrollsiffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Function LagSjekksiffer(grunnlag) | |
Dim isOne As Boolean | |
Dim controlNumber As Integer | |
Dim length As Integer | |
Dim i As Integer | |
isOne = False | |
controlNumber = 0 | |
length = Len(grunnlag) | |
For i = 0 To length - 1 | |
Dim sifferIndex As Integer | |
Dim siffer As String | |
Dim intNumber As Integer | |
Dim sum As Integer | |
sifferIndex = length - i | |
siffer = Mid(grunnlag, sifferIndex, 1) | |
intNumber = CInt(siffer) | |
If isOne Then | |
sum = intNumber | |
Else | |
sum = 2 * intNumber | |
End If | |
If (sum > 9) Then | |
sum = (sum Mod 10) + 1 | |
End If | |
isOne = Not isOne | |
controlNumber = controlNumber + sum | |
Next | |
If (10 - (controlNumber Mod 10)) Mod 10 = 0 Then | |
LagSjekksiffer = 0 | |
Else | |
LagSjekksiffer = 10 - (controlNumber Mod 10) | |
End If | |
End Function | |
Function LeggTilSjekksiffer(grunnlag) | |
Dim sjekksiffer As String | |
sjekksiffer = LagSjekksiffer(grunnlag) | |
LeggTilSjekksiffer = grunnlag & sjekksiffer | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment