[Solved] Code128 generated barcode not scanning.

Kenny H

New member
The barcode is being used to pair a ZEBRA ZQ520 printer and Cordless RS507 Ring Scanner to each other. The barcode you scan is stored in the printers settings. The barcode is generated and printed from the ZEBRA ZQ250 printer after being connected to the computer by USB Cable and navigating to the label configuration setting. Each printer has its own unique pairing barcode internally stored. The ZEBRA paring label is not printed crisp but it does scan with no issues while paring. I am attaching a picture of the ZEBRA printed barcode so you can see what it looks like and excel screenshots of the generated barcode. I am not sure how much the original barcode will help because the lines are not crip but hopefully you can see they do not match. When I scanned the barcode app it verifies it is a CODE128 Barcode. I have also attached several other screen shots that will hopefully help to show what I did. Thank you so much for your help and time!!Download (5).PNG Thank you so much for your help and time!!

download (2).pngdownload (3).pngdownload (4).pngdownload (1).pngdownload.png
 
The barcode is being used to pair a ZEBRA ZQ520 printer and Cordless RS507 Ring Scanner to each other. The barcode you scan is stored in the printers settings. The barcode is generated and printed from the ZEBRA ZQ250 printer after being connected to the computer by USB Cable and navigating to the label configuration setting. Each printer has its own unique pairing barcode internally stored. The ZEBRA paring label is not printed crisp but it does scan with no issues while paring. I am attaching a picture of the ZEBRA printed barcode so you can see what it looks like and excel screenshots of the generated barcode. I am not sure how much the original barcode will help because the lines are not crip but hopefully you can see they do not match. When I scanned the barcode app it verifies it is a CODE128 Barcode. I have also attached several other screen shots that will hopefully help to show what I did. Thank you so much for your help and time!!View attachment 516 Thank you so much for your help and time!!

View attachment 511View attachment 512View attachment 513View attachment 514View attachment 515
Hello KENNY H

Thanks for reaching out and posting your issue in Excel Forum. You described that your generated barcode does not work. You also mentioned it does not 100% match the master barcode. Assuming the ZEBRA printed barcode is the master barcode. I have scanned the ZEBRA printed barcode. I found a string against the barcode, and that is B48A4938F2007 as well. You have generated a barcode from a string, and that is 48A4938F2007. If I scan the barcode you generated in Excel, I get 48A4938F2007 as expected.

User-Defined Function:
Code:
Public Function Code128(SourceString As String)
    Dim Counter As Integer
    Dim CheckSum As Long
    Dim mini As Integer
    Dim dummy As Integer
    Dim UseTableB As Boolean
    Dim Code128_Barcode As String
    If Len(SourceString) > 0 Then
        For Counter = 1 To Len(SourceString)
            Select Case Asc(Mid(SourceString, Counter, 1))
                Case 32 To 126, 203
                Case Else
                    MsgBox "Invalid character in barcode string" & vbCrLf & vbCrLf & "Please only use standard ASCII characters", vbCritical
                    Code128 = ""
                    Exit Function
            End Select
        Next
        Code128_Barcode = ""
        UseTableB = True
        Counter = 1
        Do While Counter <= Len(SourceString)
            If UseTableB Then
                mini = IIf(Counter = 1 Or Counter + 3 = Len(SourceString), 4, 6)
                GoSub testnum
                If mini% < 0 Then
                    If Counter = 1 Then
                        Code128_Barcode = Chr(205)
                    Else
                        Code128_Barcode = Code128_Barcode & Chr(199)
                    End If
                    UseTableB = False
                Else
                    If Counter = 1 Then Code128_Barcode = Chr(204)
                End If
            End If
            If Not UseTableB Then
                mini% = 2
                GoSub testnum
                If mini% < 0 Then
                    dummy% = Val(Mid(SourceString, Counter, 2))
                    dummy% = IIf(dummy% < 95, dummy% + 32, dummy% + 100)
                    Code128_Barcode = Code128_Barcode & Chr(dummy%)
                    Counter = Counter + 2
                Else
                    Code128_Barcode = Code128_Barcode & Chr(200)
                    UseTableB = True
                End If
            End If
            If UseTableB Then
                Code128_Barcode = Code128_Barcode & Mid(SourceString, Counter, 1)
                Counter = Counter + 1
            End If
        Loop
        For Counter = 1 To Len(Code128_Barcode)
            dummy% = Asc(Mid(Code128_Barcode, Counter, 1))
            dummy% = IIf(dummy% < 127, dummy% - 32, dummy% - 100)
            If Counter = 1 Then CheckSum& = dummy%
            CheckSum& = (CheckSum& + (Counter - 1) * dummy%) Mod 103
        Next
        CheckSum& = IIf(CheckSum& < 95, CheckSum& + 32, CheckSum& + 100)
        Code128_Barcode = Code128_Barcode & Chr(CheckSum&) & Chr$(206)
    End If
    Code128 = Code128_Barcode
    Exit Function
testnum:
        mini% = mini% - 1
        If Counter + mini% <= Len(SourceString) Then
            Do While mini% >= 0
                If Asc(Mid(SourceString, Counter + mini%, 1)) < 48 Or Asc(Mid(SourceString, Counter + mini%, 1)) > 57 Then Exit Do
            mini% = mini% - 1
            Loop
        End If
        Return
End Function


Both ZEBRA-String and Your-String are not the same in length. You have missed a character B in the string when generating barcodes in Excel. That's why you have encountered those issues. The barcodes generated by you in Excel neither work nor match ZEBRA printed barcode.

OutputKenny H.png

I recommend you go through the below Article with more focus by clicking the following link.
How to Generate Code 128 Barcode Font for Excel (With Easy Steps)

You can also visit some other posts to learn more about the Barcodes in Excel.

Regards
Lutfor Rahman Shimanto
 
Last edited:
The barcode is being used to pair a ZEBRA ZQ520 printer and Cordless RS507 Ring Scanner to each other. The barcode you scan is stored in the printers settings. The barcode is generated and printed from the ZEBRA ZQ250 printer after being connected to the computer by USB Cable and navigating to the label configuration setting. Each printer has its own unique pairing barcode internally stored. The ZEBRA paring label is not printed crisp but it does scan with no issues while paring. I am attaching a picture of the ZEBRA printed barcode so you can see what it looks like and excel screenshots of the generated barcode. I am not sure how much the original barcode will help because the lines are not crip but hopefully you can see they do not match. When I scanned the barcode app it verifies it is a CODE128 Barcode. I have also attached several other screen shots that will hopefully help to show what I did. Thank you so much for your help and time!!View attachment 516 Thank you so much for your help and time!!

View attachment 511View attachment 512View attachment 513View attachment 514View attachment 515
Dear KENNY H

The procedure you applied in the generation of barcodes is excellent. Assuming you are using mentioned user-defined function implemented in VBA.

However, I can give you an event procedure to improve your method. This event procedure will format the cell to Code 128 when you call the function named Code128 in a cell. This event helps to generate a barcode without manually setting the font as Code 128. All you have to do is to right-click on the sheet name tab >> click on View Code.
Right-Click on sheet name tab.png

Later, insert the following code >> and Save.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim appliedFunction As String
    Dim searchString As String
    Dim targetCell As Range

    searchString = "Code128"

    If Target.HasFormula Then
        appliedFunction = Mid(Target.Formula, 2)

        If InStr(1, appliedFunction, searchString, vbTextCompare) > 0 Then
            Set targetCell = Target.Cells(1)
            With targetCell.Font
                .Name = "Code 128"
                .Size = 50
            End With
        End If
    End If

End Sub

Insert the event procedure and Save.png

I have attached the solution Workbook with you. Please let me know if you have any further questions or if there's anything else I can assist you with.

Regards
Lutfor Rahman Shimanto
 

Attachments

  • Kenny H (SOLVED).xlsm
    23.7 KB · Views: 3
Hi,
I'm trying to use the vb code you posted in the article "How to Generate Code 128 Barcode Font for Excel"
In my case not work: When i have a string with two consecutive zeros the code generate a empty character...
Second question: can I use it with a 128C font?
can you help me?
 

Attachments

  • Barcode1.JPG
    Barcode1.JPG
    16.6 KB · Views: 1
  • Barcode2.JPG
    Barcode2.JPG
    45.7 KB · Views: 1

Online statistics

Members online
1
Guests online
19
Total visitors
20

Forum statistics

Threads
278
Messages
1,198
Members
473
Latest member
tu2815
Top