Excel中的条形码生成器宏无法识别文本中的空格-“无效字符”

问题描述 投票:0回答:1

我有一个在excel中运行的条形码生成器宏,但是在运行宏时,由于文本中包含空格,所以我始终以“无效字符”运行。

IE:史密斯,约翰

我希望能够在excel中生成条形码,然后将其扫描到我们的学习者管理系统中,但是要包含空格,这样我们可以更轻松地搜索学生。

Sub Main()

    'Initialize variables
    Dim i, j, last_row, temp
    Dim start_a, start_frame, stop_frame, check_digit
    Dim ascii_column, ascii_value, ascii_length, ascii_code
    Dim barcode_column, barcode_value

    'Define variables
    ascii_column = 1
    barcode_column = 2
    check_digit = 0
    barcode_value = ""
    start_a = 103  'Start A Code (in Code 128 format)
    start_frame = 153  'Start A Code (in ASCII format)
    stop_frame = 156  'Stop A Code (in ASCII format)

    'Begin

    'Get last row
       Cells(1, ascii_column).Select
       Selection.End(xlDown).Select
        last_row = ActiveCell.Row
    If last_row > 10000 Then
    Range("A2").Select
    MsgBox ("There doesn't appear to be anything to do. Try again."), vbOKOnly
        Exit Sub
    End If

    'Setup barcode column
    Columns("B:B").Select
    With Selection.Font
    .Name = "Code128bWin"
    .Size = 20
    End With
    Rows("1:1").Select
    With Selection.Font
    .Name = "Arial"
    .Size = 10
    End With

    'Go to each row
    For i = 2 To last_row

    'Get user value and obtain length
    Cells(i, ascii_column).Select
    ascii_value = Trim(ActiveCell.Value)
    ascii_length = Len(ascii_value)

    'Calculate check digit
    For j = 1 To ascii_length

        'Convert value to ASCII
        temp = Asc(Mid(ascii_value, j, 1))

        'Convert ASCII to CODE128
        Select Case temp

            Case 128
                ascii_code = 0

            Case 33 To 126
                ascii_code = temp - 32

            Case 127 To 156
                ascii_code = temp - 50

            Case Else
                MsgBox ("Invalid character detected. Check input and try again."), vbOKOnly
                Exit Sub

        End Select

        'Aggregate check digit
        check_digit = check_digit + (ascii_code * j)

    Next j

    'Add start frame to check digit and mod 103
    check_digit = (start_a + check_digit) Mod 103

    'Convert CODE128 value back to ASCII
    If check_digit = 0 Then
        check_digit = 128

    ElseIf check_digit <= 94 Then
        check_digit = check_digit + 32

    Else
        check_digit = check_digit + 50

    End If

    'Combine guard bars, value, and check digit
    barcode_value = Chr(start_frame) & ascii_value & Chr(check_digit) & Chr(stop_frame)

    'Write out barcode value
    Cells(i, barcode_column).Select
    ActiveCell.Value = barcode_value

    'Reset values
    barcode_value = "": check_digit = 0

Next i

Range("A2").Select

End Sub
excel vba barcode-scanner
1个回答
0
投票

此论坛针对具有编程经验的人,这些人的代码无法正常工作。超级用户可能是一个更好的论坛。

[您还需要注意,<space>字符不是128条码字体的ISO规范的一部分。但是至少有一种实现为此使用194

如果正在使用的实现做到这一点,则可以使用此修改后的Select Case段,以允许翻译<space>字符:(ASCII Code 32]

Select Case temp

    Case 128
        ascii_code = 0

    Case 33 To 126
        ascii_code = temp - 32

    Case 127 To 156
        ascii_code = temp - 50

    Case 32
        ascii_code = 194

    Case Else
        MsgBox ("Invalid character detected. Check input and try again."), vbOKOnly
        Exit Sub

End Select

如果没有实现,则需要为<space>找到正确的代码,或者编写自己的代码。

© www.soinside.com 2019 - 2024. All rights reserved.