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.
IE: Smith, John
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.
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