简体   繁体   English

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

[英]Barcode Generator macro in Excel not recognizing space in text-- "Invalid character"

I have a barcode generator macro running in excel, but I keep running in an "invalid character" when I run the macro because the text includes a space.我有一个在 excel 中运行的条形码生成器宏,但是当我运行宏时,我一直以“无效字符”运行,因为文本包含一个空格。

IE: Smith, John IE:史密斯,约翰

I'd like to be able to generate a barcode in excel and then scan the barcode into our learner management system, but to include the spaces so it's easier for us to search for our students.我希望能够在 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

This forum is directed at those with programming experience, who have code that is not working.该论坛面向具有编程经验但代码无法正常工作的人。 Probably superuser would have been a better forum.超级用户可能是一个更好的论坛。

You also need to be aware that the <space> character is NOT part of the ISO specification for the 128 barcode font.您还需要注意<space>字符不是 128 条码字体的 ISO 规范的一部分。 But at lease one implementation uses 194 for that.但是至少有一个实现为此使用了194

If the implementation you are using does that, you can use this modified Select Case segment, to allow for translation of the <space> character: ( ASCII Code 32 )如果您正在使用的实现这样做,您可以使用此修改后的Select Case段,以允许转换<space>字符:( ASCII 代码 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

If your implementation does not, you will need to either find the correct code for the <space> , or write your own.如果您的实现没有,您将需要为<space>找到正确的代码,或者编写您自己的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM