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 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.
- After that, select Insert and then Module in the Visual Basic window.
- At this moment, the Module window will appear.
- 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
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:
Here, the function will convert the data of Cell C5 into a barcode.
- In the following step, press Enter to see the result.
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.
- 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)
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.
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 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.
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.
Thanks for your comment. Excel omits the leading zero by default. So, it’s not possible to get the desired barcode.
• To solve this, you must add an apostrophe (‘) in front of the leading zero.
• So, select Cell C5 in the VBA sheet of the workbook.
• Type ‘02628107336750 in Cell C5.
• Press Enter.
• You will get your results in Cell D5.
I hope this will help you to solve your problem.
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?
Thanks for your comment. Code 128 barcode font has 106 unique representations and supports standard ASCII characters. Unfortunately, Õ and Œ – these two characters are not supported by code 128 barcode font. But you can try other barcode fonts to solve this issue. You can use the free online barcode generator for that purpose. I hope this will help you to solve your problem.
How can I generate the barcode with the ASCII characters below the barcode on same cell.
Thanks for your comment. You can’t generate the text below the barcode with the code 128 font we used here. But you can use the Libre Barcode 128 Text font for that. Also, you can follow this article https://www.exceldemy.com/generate-barcode-numbers-in-excel/ to generate characters below the barcode. I hope this will help you to solve your problem. Please let us know your queries if you face any issues.
I’m hoping to insert a Horizontal Tab in between two strings using your method, “XXXX.1TAB123456” with the intent of using one barcode to enter two separate fields
Is there a special character or function to be implemented to do this? Or is it even possible?
Thanks for your comment. You can insert a Tab using “~009” inside the string. For example, XXXX.1TAB123456 can be written as XXXX.1~009123456. But unfortunately, it will not show two separate fields in one barcode. From my understanding, you need to use other barcode fonts for that purpose.
Otherwise, you can look at the solution below. But this solution will not provide you with one barcode. Here, we tried to concatenate two separate barcodes in one cell. Here, we will use the dataset below.
Let’s follow the steps below for the solution.
● Firstly, change the fields into barcode strings.
● To do so, select Cell D5 and type the formula below:
● Also, do the same for the second field.
● After that, select Cell B7 and type the formula below:
● Hit Enter to see the result.
● Now, select Cell B7 again and change the font theme to Code 128.
● Also, adjust the font size.
● Finally, you will get results like the picture below.
I hope this solution will help benefit you to some extent. Please let us know if you face any other issues.
Is there similar VBA code for Microsoft Word? Trying to get this font(code 128) to work with a mail merge.
Thanks for your comment. Unfortunately, there is no similar VBA code for Microsoft Word. You need to use the VBA in Excel to get the barcode using Code 128. But you can follow the link below to use code 128 barcode font in Microsoft Word.
I hope this will help you to solve your problem. Please let us know if you face any other issues.
When I’m trying to convert numbers to barcode im getting errors. For example number 192697
gives Í3:ĹWÎ where Ĺ is not barcode, the same is with 194898 which gives output Í3PĆĘÎ – when font changed to code128 it still shows ĆĘ in middle. How can I solve that – there must be some error in VBA…
Thanks for your comment. Sorry to hear that you are not getting the desired output. I have tried to convert 192697 and 194898 to barcodes using the same code. In my case, it worked. You can see the result below:
I am also attaching the Excel file below:
Moreover, the VBA code is not error-free. It can introduce errors sometimes. You can copy the code and paste it into a new workbook. It may help you to get the desired results.
I hope this will help you to solve your problem. Please let us know if you have other queries.
Hello. Thank you for posting this. I have folowed the procedure and it doesnt generate the desired readable barcode. It creates the correct text: ÌA00M2Ç@A8Î
but when the font is changed to code 128 it creates a barcode that is not scannable.
Thanks for your comment. This is Mursalin from Exceldemy. I am not quite sure why you are getting a barcode that is not scannable. Because in my case, after changing the text to Code 128, I am getting the desired scannable barcode.
It will be helpful for me if you provide the Excel file or an image in the comment section. Or you can send the Excel file to [email protected]. I hope I can give a solution after watching the file.
This was not very helpful for people like me who do not have the Developer tab on their menu in Excel. I don’t understand why my characters for the barcode is %P+999-807989$ and when I change the font setting on that field to code 128, the barcode scanner cannot read it.
Sorry to hear about your problem. I am replying to you on behalf of Exceldemy. The Developer tab is not included in the ribbon by default. But you can easily add it. To add the Developer tab, please the steps of the link below:
Get Developer Tab
After inserting the Developer tab, follow the steps of the article. If you follow the steps correctly, you will get the scannable barcodes. You must insert the VBA using the Developer tab as stated in STEP 2 for getting the correct characters.
I’m on MAC and just opening up your example if I click on the cell and then off changes the bar code.
Note the << and the CE below. This is on your data first row. A00M23233.
Does this have to do with MAC UF8? Anyway to fix it?
You may not get the desired output if you download the excel file attached to this article and perform operations.
1. You must download the barcode font. The link is in the 1st step of this article
2. Also, code 128 has 106 distinct characters. Œ is not available in code 128.
Maybe these are the 2 issues that need to be resolved.
However, if these don’t satisfy you, please let us know.
Have a great day!
What a cool bit of code! It’s working fine so far and it was truly free.
I’m using code 128 in Excel. It’s pretty simple for the SKU’s we are working with.
Thank you So Much!
I’ll reply back if I find an issue and will bookmark this page to check for updates.
Thanks for your comment. We are very glad to know that we could be of help to you. Let us know if you have any queries.
Hi Mursalin. Becasue i have problem with formula while im generating code128 characters i download also workbook from your entry Oct 10, 2022 at 4:31 PM. Problem is also there. For meny antry im receiving not recognizing characters in barcode. You can try for example 456987321596328 Go you have any idea how to solve it?
Thank you for sharing your query. To solve this, you can try another formula instead after copying the Code 128 Font in the C:\Windows\Fonts folder.
First, insert this formula for the data 456987321596328 and press Enter.
Then, change the Font to Code 128 and the Size to 36 for creating the barcode.
Make sure, your input data is in Text format before applying the formula.
Moreover, VBA is not error-free. So I suggest you create a new Excel-Macro Enabled Workbook and copy-paste the VBA code to create your own barcode.
I hope this will help you to solve your problem. Please let us know if you have other queries.
Is it possible to encode keyboard functions into barcode such as “TAB” and “Return” key?
Of course, it’s possible to solve your mentioned problem!
You will need to get the ASCII code of the respective keys. Now, open a new Excel file and follow all the steps written in this article, i.e. downloading Barcode fonts 128, installing them, creating a UDF, and the next steps. Now insert the ASCII codes of the respective keys and you will get your desired Barcode. See what we have got.
So, what you need to do yourself, just finding the ASCII codes! What else to do are already mentioned in this article.
Note: You may need to open a new file because as per the new update of Excel, a VBA code will be disabled in a downloaded xlsm file. Or you can solve the problem following this way.
very detailed steps.
Thanks for your appreciation.
Is there anyway to make this work for new excel files, or do you have to go through these steps and add the VB macro to any excel file that you wish to had 128 barcodes too?
Thank you, Rob, for your comment. To make the function available all the time, you can copy the code and paste the VBA code in a workbook. Then, save that workbook as Excel Add-in (*.xlam) file. Then, you will need to enable this add-in for a new Excel file. Thus, the function will be available as long as the add-in is enabled. Additionally, you will need to install the font beforehand.
Let’s assume, you have exported the Excel file as “code 128.xlam” file. Then, you will need to enable this (File > Options > Add-ins > select Go).
Then, you will need to enable the exported file name (in our case it is “code 128“) and press OK.
After doing this, whenever you create a new Excel file, you will be able to use this “CODE128()” function without copying the VBA code.