How to Generate Code 128 Barcode Font for Excel (With Easy Steps)

In this article, we will learn to generate code 128 barcode font for Excel. There are some methods to use code 128 barcode font in Excel, but most are challenging to apply and don’t work on all versions of Excel. Today, we will demonstrate a method to generate code 128 barcode font with easy steps in Excel. After reading the article, you will be able to use code 128 barcode font very easily. So, without any delay, let’s start the discussion now.


Download Practice Book

You can download the practice book from here.


What Is Code 128 Barcode Font?

Code 128 is a modern and famous barcode font. Its popularity is increasing day by day because it is a high-density barcode font that supports alphanumeric characters.

Generally, code 128 consists of seven sections. They are:

  • Quiet Zone
  • Start Symbol
  • Encoded Data
  • Check Symbol
  • Stop Symbol
  • Final Bar
  • Quiet Zone

The code 128 barcode font has 3 subsets. They are described briefly below:

  • Code 128A: It supports ASCII without lowercase characters.
  • Code 128B: It supports ASCII without the initial special characters.
  • Code 128C: This subset supports Numeric Values.

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel

To explain the steps, we will use a dataset that contains information about some products and their data. Using the method, we will try to generate barcodes with Code 128 font for the data of each product.


STEP 1: Download Code 128 Font

  • First of all, you need to download Code 128 You can download the font from this link.
  • After that, extract the downloaded folder to the C:\Windows\Fonts folder.
  • Otherwise, unzip the downloaded folder, copy the Code 128 font and paste it to the C:\Windows\Fonts folder.
  • Also, select Continue if the administrator permissions window appears.

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel


STEP 2: Apply VBA Code

  • Secondly, go to the Developer tab in the ribbon and select Visual Basic.
  • As a result, it will open the Visual Basic window.

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel

  • After that, select Insert and then Module in the Visual Basic window.
  • At this moment, the Module window will appear.

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel

  • Now, we need to type a code in the Module window.
  • You can copy it from below and paste it into the Module window:
Option Explicit
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

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel

VBA Code Explanation:

In this code, we will create a function that will convert a string to barcodes. Here, we will use the Code 128 font.

  • The input parameter is a string.
  • In the output, we will get a barcode in the Code 128 font if the string is valid.
  • Otherwise, it will display an empty string.
Public Function Code128(SourceString As String)

This part denotes the function name and it is Code128(). You need to insert the string inside the parentheses.

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

These are the variables that will be used in the code.

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

In this section, the code will check for valid characters. If it finds no valid character, then, it will ask the user to use standard ASCII characters.

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

Here, this part calculates the value of the CheckSum variable.

CheckSum& = IIf(CheckSum& < 95, CheckSum& + 32, CheckSum& + 100)
Code128_Barcode = Code128_Barcode & Chr(CheckSum&) & Chr$(206)
End If

In this part, the code calculates the CheckSum ASCII code. After adding the ASCII code, it moves to the next part.

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

In the last part, the code will check for the numeric values inside the given string.

This VBA code was found from myonlinetraininghub.com.

  • After typing the code, press Ctrl + S to save it.
  • In the following step, close the Visual Basic window.

STEP 3: Use Code 128 Function

  • Thirdly, we need to use the function that we created by applying the VBA.
  • In order to do that, select Cell D5 and type the formula below:
=Code128(C5)

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel

Here, the function will convert the data of Cell C5 into a barcode.

  • In the following step, press Enter to see the result.

Step-by-Step Procedures to Generate Code 128 Barcode Font for Excel


STEP 4: Change Font Theme and Size

  • In the fourth step, you need to change the font theme and size.
  • For that purpose, select Cell C5.
  • Then, go to the Home tab and select Code 128 in the font theme box.
  • Also, select 36 in the font size box.


STEP 5: Resize Column Width and Row Height

  • After changing the font theme and size, we need to resize column width and row height.
  • In our case, we have set the width of Column D to 30 and the Row Height to 50.


STEP 6: Use Fill Handle to Copy Formula

  • In the following step, select Cell D5 and drag the Fill Handle down to the rest of the cells.


Final Output

  • Finally, change the Row Height of Row 6,7,8, and 9 to 50.
  • After finishing all the steps, you will see results like the picture below.

Read More: How to Use Code 39 Barcode Font for Excel (with Easy Steps)


Conclusion

In this article, we have demonstrated step-by-step procedures to generate Code 128 Barcode Font for Excel. I hope this article will help you to create barcodes easily. Moreover, you can use the workbook to practice. In order to do so, download the workbook. We have added the workbook at the beginning of the article. Also, you can visit the ExcelDemy website for more articles like this. Last of all, if you have any suggestions or queries, feel free to ask in the comment section below.


Related Articles

Mursalin

Mursalin

Hi there! This is Mursalin. I am an Excel and VBA content developer as well as an electrical and electronics engineer. I am always motivated to gather knowledge from different sources and find solutions to problems in easier ways. I am currently working and doing research on Microsoft Excel. Here I will be posting articles related to Microsoft Excel.

4 Comments
  1. Hi Martin,

    I’m looking at this as an ad-hoc solution for low-use barcodes. When comparing to current barcode examples, the start-tags do not appear correctly on my barcodes.

    Thanks,
    Dom

    • Hello, Dom!
      Thanks for your feedback. If you are facing any problems generating code 128 barcode font in Excel, then you can ask here. We will try to reply to you as soon as possible.
      Thanks!

  2. Hello. I’m attempting to create a barcode that also include the numeric 0. 02628107336750 as an example. How can I use this article to achieve this result.

  3. I am using your VB code to generate the following barcode output – Õ46G4YŒ
    However the Õ and the Œ, at the beginning and the end do not change into barcodes when the code128 font is applied, they stay as alphanumberic characters. What am I doing wrong?

Leave a reply

ExcelDemy
Logo