エクセルマニア

エクセル JANコードの符号化 VBAの解説

TOP > バーコード(JAN・CODE39・NW-7)の印刷 > JANコード用の文字列関数
このエントリーをはてなブックマークに追加
JANコードの符号化に関する詳細は、"JANバーコード"の JANバーコード使用説明書.pdf に記載されています。

JANコード用の文字列関数

自作の code2jan 関数は 「4958717100529」のような見慣れたコードをバーコード生成用の文字列「X9F87BHYbaafcjZ」のように変換する関数です。

Public Function code2jan(code As String)
    Dim jan As String
    Dim s1 As String
    Dim s2 As String
    Dim p(10) As String
    Dim ps As String
    Dim b1 As String
    Dim b2 As String
    
    b1 = Left(code, 7)
    b2 = Mid(code, 7, 7)
    
    p(0) = "111111"
    p(1) = "110100"
    p(2) = "110010"
    p(3) = "110001"
    p(4) = "101100"
    p(5) = "100110"
    p(6) = "100011"
    p(7) = "101010"
    p(8) = "101001"
    p(9) = "100101"
    
    s1 = Left(code, 1)
    
    If s1 = "" Then
        code2jan = ""
        Exit Function
    End If
    
    p2 = p(s1)
    
    jan = "X"
    
    For i = 2 To 7
        jan = jan & left_code(Mid(b1, i, 1), Mid(p2, i - 1, 1))
    Next i
    
    jan = jan & "Y"
    
    For i = 8 To 13
        jan = jan & right_code(Mid(b2, i - 6, 1))
    Next i
    
    jan = jan & "Z"
    
    code2jan = jan
    
End Function

'左側パリティ
Public Function left_code(s As String, b As String) As String
    Dim r As String
    If b = "0" Then
        If s = "0" Then r = "A"
        If s = "1" Then r = "B"
        If s = "2" Then r = "C"
        If s = "3" Then r = "D"
        If s = "4" Then r = "E"
        If s = "5" Then r = "F"
        If s = "6" Then r = "G"
        If s = "7" Then r = "H"
        If s = "8" Then r = "I"
        If s = "9" Then r = "J"
    Else
        r = s
    End If
    left_code = r
End Function

'右側パリティ
Public Function right_code(s As String) As String
    Dim r As String
    If s = "0" Then r = "a"
    If s = "1" Then r = "b"
    If s = "2" Then r = "c"
    If s = "3" Then r = "d"
    If s = "4" Then r = "e"
    If s = "5" Then r = "f"
    If s = "6" Then r = "g"
    If s = "7" Then r = "h"
    If s = "8" Then r = "i"
    If s = "9" Then r = "j"
    right_code = r
End Function




書籍  IT系資格  統計解析  データ分析  マーケティング