By: Shamima Sultana

In reply to Kurt Kruger.

Hello Kurt Kruger,

Our existing code generate Code 128 barcodes, primarily focusing on Code 128B, and can switch to Code 128C for efficient numeric encoding when needed. Modified the existing VBA code to include Code 128A encoding, which includes upper-case letters, control characters, and special characters. The start, checksum, and stop characters are added to generate a valid Code 128A barcode.

Option Explicit
Public Function Code128A(SourceString As String) As String
Dim Counter As Integer
Dim CheckSum As Long
Dim mini As Integer
Dim dummy As Integer
Dim Code128A_Barcode As String

If Len(SourceString) > 0 Then
For Counter = 1 To Len(SourceString)
Select Case Asc(Mid(SourceString, Counter, 1))
Case 0 To 95
Case Else
MsgBox "Invalid character in barcode string" & vbCrLf & vbCrLf & "Please only use characters supported by Code 128A", vbCritical
Code128A = ""
Exit Function
End Select
Next

Code128A_Barcode = Chr(103) ' Start code for Code 128A

For Counter = 1 To Len(SourceString)
dummy = Asc(Mid(SourceString, Counter, 1))
If dummy >= 0 And dummy <= 31 Then
Code128A_Barcode = Code128A_Barcode & Chr(dummy + 64)
ElseIf dummy >= 32 And dummy <= 95 Then
Code128A_Barcode = Code128A_Barcode & Chr(dummy - 32)
End If
Next

' Calculate checksum
CheckSum = 103 ' Start code value
For Counter = 1 To Len(SourceString)
dummy = Asc(Mid(SourceString, Counter, 1))
If dummy >= 0 And dummy <= 31 Then
dummy = dummy + 64
ElseIf dummy >= 32 And dummy <= 95 Then
dummy = dummy - 32
End If
CheckSum = (CheckSum + Counter * dummy) Mod 103
Next

' Append checksum character
If CheckSum < 95 Then
Code128A_Barcode = Code128A_Barcode & Chr(CheckSum + 32)
Else
Code128A_Barcode = Code128A_Barcode & Chr(CheckSum + 100)
End If

' Append stop character
Code128A_Barcode = Code128A_Barcode & Chr(106)
End If

Code128A = Code128A_Barcode
End Function

Regards
ExcelDemy