Skip to content

Instantly share code, notes, and snippets.

@aklefdal
Last active Dec 6, 2017
Embed
What would you like to do?
VBA-funksjoner for å gjøre MOD10-generering av kontrollsiffer
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