簡體   English   中英

Excel 中的條形碼生成器宏無法識別文本中的空格--“無效字符”

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

我有一個在 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

該論壇面向具有編程經驗但代碼無法正常工作的人。 超級用戶可能是一個更好的論壇。

您還需要注意<space>字符不是 128 條碼字體的 ISO 規范的一部分。 但是至少有一個實現為此使用了194

如果您正在使用的實現這樣做,您可以使用此修改后的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

如果您的實現沒有,您將需要為<space>找到正確的代碼,或者編寫您自己的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM