How to Generate Code 128 Barcode Font for Excel

Note: The given VBA user-defined function Code128 can only be used in the Windows operating system. It will not work properly in Mac OS.

What Is the Code 128 Barcode Font?

Code 128 is a modern and famous barcode font. It is a high-density barcode font that supports alphanumeric characters.

Code 128 consists of seven sections:

  • 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.

We will use a dataset that contains information about some products and their data. We will generate barcodes with Code 128 font for the data of each product.


Step 1 – Downloading the Code 128 Font

  • Extract the downloaded folder to the C:\Windows\Fonts folder. Alternatively, unzip the downloaded folder, copy the Code 128 font and paste it to the C:\Windows\Fonts folder.
  • Select Continue if the administrator permissions window appears.

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

Read More: How to Generate Barcode Numbers in Excel 


Step 2 – Using Excel VBA to Create a User-Defined Function

  • Go to the Developer tab in the ribbon and select Visual Basic.
  • This will open the Visual Basic window.

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

  • Select Insert and choose Module in the Visual Basic window.
  • The Module window will appear.

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

  • Insert the following code 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:

  • 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: Code128(SourceString). You need to insert a string inside the parentheses when using it.

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

The code will check for valid characters. If it finds no valid character, 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

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

The code will check for the numeric values inside the given string.

  • Press Ctrl + S to save the file.
  • Close the Visual Basic window.

Read More: How to Use EAN 13 Barcode Generator in Excel


Step 3 – Using the Code 128 Function

  • Select Cell D5 and insert the formula below:
=Code128(C5)

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

  • Press Enter to see the result.

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

Read More: How to Calculate Barcode Check Digit with Excel Formula


Step 4 – Changing Excel Theme Font and Size

  • Select Cell C5.
  • Go to the Home tab and select Code 128 in the font theme box.
  • Select 36 in the font size box.

Read More: How to Add Barcode Font in Excel 


Step 5 – Resizing the Column Width and Row Height

  • We have set the width of Column D to 30 and the Row Height to 50.

Step 6 – Using the Fill Handle to Copy the Formula

  • Select Cell D5 and drag the Fill Handle down to the rest of the cells.

Read More: How to Create Barcode Using 3 of 9 Font in Excel


Final Output of the Code 128 Barcode Font for Excel

  • Change the Row Height of Row 6,7,8, and 9 to 50.

Read More: How to Use Code 39 Barcode Font for Excel


Download the Practice Book


Related Articles


<< Go Back to Barcode in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

68 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!

      • Hello, thank you very much for your contribution, it is great. But is it possible to add a function so that when the same code is read twice, it throws me a message saying that it is duplicated?

        • Avatar photo
          Mashhura Jahan Jun 21, 2023 at 9:41 AM

          Hi OMAR DEL VALLE,
          Thanks for your comment. I am replying to you on behalf of ExcelDemy. You can apply the following formula for finding duplicate barcodes.

          =IF(COUNTIF($C$5:C5,C5)>1,"Duplicate",Code128(C5))

          But, here the duplicates are also in Code 128 font to change that follow these steps.
          • Right-click on the sheet name >> select View Code.

          • Write the following code.

          Private Sub Worksheet_SelectionChange(ByVal Target As Range)
          For row_no = 1 To 100
              If Range("D5").Cells(row_no, 1).Value = "Duplicate" Then
                  Range("D5").Cells(row_no, 1).Font.Name = "Calibri"
                  Range("D5").Cells(row_no, 1).Font.Size = 11
              End If
          Next
          End Sub

          • Now, just drag the Fill Handle and you will get your desired result.

          I hope this will help you to solve your problem. Please let us know if you have other queries.
          Regards
          Mashhura Jahan
          ExcelDemy.

  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.

    • Dear CHRIS,
      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.
      Thanks.

  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?

    • Dear NEIL,
      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.
      Thanks.

  4. Hello!
    How can I generate the barcode with the ASCII characters below the barcode on same cell.
    Thanks

    • Hello CRISNA,
      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.
      Thanks.

  5. Hello,
    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?
    Thank you!

    • Dear MICHAEL,
      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:
      =Code128(B5)

      ● Also, do the same for the second field.

      ● After that, select Cell B7 and type the formula below:
      =CONCAT(D5,CHAR(9),E5)

      ● 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.
      Thanks!

  6. Is there similar VBA code for Microsoft Word? Trying to get this font(code 128) to work with a mail merge.

  7. Hi RICK,

    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.
    https://www.exceldemy.com/print-barcode-labels-in-excel/#Step_4_Generating_and_Printing_Barcode_Labels

    I hope this will help you to solve your problem. Please let us know if you face any other issues.
    Thanks!

  8. 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…

    • Hi DAWID,

      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:
      Answer.xlsm
      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.
      Thanks!

  9. 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.

    Any thoughts?

    • Hi KIERAN,

      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.
      Thanks!

  10. 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.

    • Hi FREEJOJOEY,

      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.

      Thanks!

  11. 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.

    ÃA00M2«@A8Œ

    Does this have to do with MAC UF8? Anyway to fix it?

  12. Hi Casey,
    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!

  13. 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.

    • Hi MYLES,

      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.

      Good luck.

  14. 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?

    • Hi Jacek,
      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.
      ="*"&B5&"*"

      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.
      Thanks!

  15. Is it possible to encode keyboard functions into barcode such as “TAB” and “Return” key?

    • Hello, CL.
      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.
      Keyboard keys to Barcode
      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.

  16. Hi Mursalin

    very detailed steps.

    Thanks!

    Peter

  17. 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?

    Thanks

    • 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.

  18. Hello, if I get the table, which says “Ambiguous name detected: Code128” after inserting the =Code128(C5), what should I do? Or what I did wrong?

    • Reply Mursalin
      Mursalin Ibne Salehin May 11, 2023 at 11:55 AM

      Hi INGRIDA,
      Thanks for your comment. Generally, the “Ambiguous name detected: Code128” error occurs when you insert the same formula in multiple Modules in Excel. In the picture below, you can see Module 1 and Module 2 contain the same code.

      In this case, you will get the “Ambiguous name detected: Code128” error.
      To solve it, you need to keep one module and remove others. To do so, right-click on the module you want to remove. Then, select Remove Module2.

      After that, select No from the pop-up message box.

      After that, you can apply the “=Code128” formula without any error. I hope this will help you to solve your problem. Please let us know if you have any other queries.

      Regards
      Mursalin,
      ExcelDemy.

  19. Hello,

    I am attempting to create a barcode with the enter function within the barcode.
    For example,

    ID: 833
    Password: 833

    I want to be able to scan the barcode and it input 833 \enter 833 \enter

    I was trying to follow the reply above by ALOK in November:
    “Hello, CL.
    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.
    Keyboard keys to Barcode
    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.”

    I don’t quite understand how they just used 9 and 10 for tab and enter respectively. I have the code working now – I am just troubled with the enter implementation.

    • Hello CALEB,

      Greetings! We appreciate you contacting us and commenting on our Excel blog post with your query. We appreciate your interest and are available to help. Enter the following VBA code in your module and correct the range in that code. Hopefully, this will implement the “Enter” function key in Excel which means the code will activate the next blank cell.

      Sub ActivateFirstBlankCell()
          Dim cell As Range
          ' Loop through each cell in the range D5:D11
          For Each cell In Range("D5:D11")
              ' Check if the cell is blank
              If cell.Value = "" Then
                  ' Activate the blank cell
                  cell.Activate
                  Exit Sub ' Exit the loop once the first blank cell is found
              End If
          Next cell
      End Sub

      The result will be like this.

      If you enter =CHAR(13) in cell A1 and =CODE128(A1) in cell B1 in Excel, the following will happen:

      1. Enter will be represented in cell A1 and will display a carriage return character, but it will not be visibly represented. A carriage return is a non-printable control character that represents a line break or the “Enter” key.
      2. In cell B1, the formula =CODE128(A1) will attempt to encode the content of cell A1 as a Code 128 barcode. However, since the content of cell A1 is a non-printable character (carriage return), it may not be directly interpretable by the CODE128 function. The CODE128 function is typically designed to encode printable ASCII characters.

      I’ll be pleased to help you further if you can give me more information or a sample of your Excel sheet if the issue continues.

      Regards

      Al Ikram Amit

      Team ExcelDemy

  20. Hello,
    I was trying to follow the response by ALOK Nov 13, 2022 at 12:04 PM
    I am using the code provided and it produces the barcode that can be read; however, I don’t understand how 9 and 10 mean tab and enter respectively. Can you provide me with an example piece of a cell that would return “8233 CR 8233 CR” in one barcode?

    • Hello, CALEB HOGUE. Thank you for your query. I understand you find difficulties using 9 and 10  as Tab and Enter. Please follow the below code to get 9 and 10 as Tab and Enter respectively.

      Sub ActivateFirstBlankCell()
      Dim cell As Range

      Sub ActivateFirstBlankCell()
          Dim cell As Range
      
          ' Loop through each cell in the range A1:A5
          For Each cell In Range("A1:A5")
              ' Check if the cell is blank
              If cell.Value = "" Then
                  ' Activate the blank cell
                  cell.Activate
                  Exit Sub ' Exit the loop once the first blank cell is found
              End If
          Next cell
      End Sub

      Also, follow the code that is already used in this article to convert “8233 CR 8233 CR” into one barcode

       

  21. Thank you for taking the time to create the code and instructions on how to make the barcode. I am encountering a problem that I hope you can help me with. My generated barcode does not work and does not match the master barcode 100%. Is there any chance you might know what I am doing wrong? Thank you again for your time!! If there is a way I can paste a copy of the master barcode that is working and the one that I am generating it may help

    • Hello, KENNY H.
      Thank you for sharing your problem with us. However, we would have liked more information about your problem. The method described in this article should work perfectly. Try to follow the methods and steps given in this article as it is. Make sure to download the proper code 128 font.
      You can email us screenshots of your problem or your workbook at [email protected]
      That would really help us to understand your problem and give a proper solution.
      You can also follow the articles below for generating barcodes in Excel.
      1. Create Barcode in Excel
      2. Generate 2D Barcode in Excel
      Have a nice day!
      Regards,
      Sourav
      Exceldemy.

  22. Thank you for the quick reply!! I tried sending an email to the address [email protected] but the email was returned saying the address was not good

  23. Thank you for this tutorial. I am having difficulty with the VBA function which does not seem to translate the data correctly.

    Your example, above, shows the following result in cell D5: ÌA00M2Ç@A8Î
    If I type this in manually, and convert the font to Code 128 the barcode is correct and readable, so my barcode font is OK.

    However, for me, the Code128 function in cell D5 returns: ÃA00M2«@A8Œ
    which gives the wrong barcode. In fact, two of the characters don’t give a barcode at all: « and Œ

    I’m running Excel v16 on MacOS 12.

    Thanks.

  24. Thank you for this article. I have a similar problem as mentioned by Casey, above, trying to use this on Excel for MacOS. The problem stems from the fact that the ANSI character set for Mac and PC are not the same.

    See for example:
    PC: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/character-set-128255
    Mac: https://www.spreadsheetweb.com/mac-os-character-set/

    This creates a problem for all the VBA steps involving CHR(). I partly fixed this issue by substituting:
    Chr(235) instead of Chr(206) Î
    Chr(234) instead of Chr(205) Í
    Chr(130) instead of Chr(199) Ç
    Chr(237) instead of Chr(204) Ç
    Chr(233) instead of Chr(200) È

    This fixes the issue with the start/stop characters. However there is still a problem with the checksum that I can’t figure out. It still translates to the wrong character, and therefore the barcode can’t be read.

    • Thank you, MATTHEW, for bringing this issue to our attention. After examining the problem, we’ve also identified that the VBA function is not accurately translating the data on Mac OS. You have rightly indicated that the discrepancy in ANSI character codes between Windows and Mac operating systems is the root cause of this problem. We also found that certain characters, such as the initial and final characters Ì and Î, have different codes on Mac and Windows systems. We’re actively working on creating a new VBA function that will function correctly on Mac too.

      Regards
      Aniruddah
      Team Exceldemy

  25. Thanks for all the support and wonderful article, i want to use “-” in my barcode can you please help. like the code “30001-WK-H-0001”.

    • Reply Bishawajit Chakraborty
      Bishawajit Chakraborty Sep 18, 2023 at 4:05 PM

      Thank you, DAWOOD, for your wonderful question.

      In Excel, you can use a barcode font to create barcodes that include hyphens (“-“). Here are the steps to do it:

      1. Download and Install a Barcode Font:

      • First, you’ll need to download and install a barcode font that supports hyphens. You can find various barcode fonts online, and some popular ones include Code 128, Code 39, or EAN-13. Download the font and install it on your computer following the font installation instructions for your operating system.

      2. Enter Your Barcode Data:

      • In an Excel cell, enter your barcode data, including the hyphens.
      • Select all the codes in the cell.
      • From the Font group in the Home tab, choose Code 128 font format.
      • Press Enter.

      • Now, you will see the following result.

      I hope this may solve your issue. Also, please follow this article to get your answer with detailed explanations.

      That’s it! You’ve created a barcode in Excel with hyphens included in the data. Remember that the actual appearance and functionality of the barcode depend on the specific barcode font you’re using, so make sure to choose a font that supports the format you need.

      Bishawajit, on behalf of ExcelDemy

  26. Hi and thank you for you article, it has been really instructive!

    I have a problem, I’m trying to add to my SSCC Code the FNC-1 Character, but the font I have downloaded (the one you’ve posted above), doesn’t read this charachter, leaving me with a blank cell.

    Moreover, above, you are talking about using the =Code128b function for FCN-1 character adding purposes, but this function does not exists.

    Can you please help me?

    • Hello Stefano,
      I’m glad you found the information helpful, but I understand you’re still encountering issues with adding the FNC-1 character to your SSCC Code using a Code 128 font.

      I apologize for any confusion. The downloaded font here can not directly convert codes into Barcodes. You need to apply the VBA code to create the function “=Code128” to transform the SSCC code into symbols. After that, using the downloaded font will work.

      For your second problem, there isn’t a built-in Excel function called “=Code128b” for adding the FNC-1 character. Excel has no native function for generating Code 128 barcodes with special characters.
      That is why we built a function named “=Code128” using the VBA code for your convenience. Follow step 2 which is Using VBA to Create User Defined Function to create the function.
      I hope the confusion is cleared and this process willwork for you now.
      Regards
      Exceldemy Team

  27. Hello I am having trouble as the vba wont run on my other computer and I cannot figure out why… can you please help? has anyone else encountered a similar issue?

  28. Reply Avatar photo
    Musiha Mahfuza Mukta Nov 15, 2023 at 12:24 PM

    Hello Daphne, the VBA code is perfectly working on my laptop. The code is fine. To run a VBA code you must follow:
    1. Save your Excel file in .xlsm format
    2. Use the Excel offline version
    3. Enable macro content, to do so right click on Excel file >> from the Context Menu Bar >> go to Properties option >> General >> Security >> Unblock
    Still, if you face the problem, then please go through this article [Fixed!] Macros Not Working in Excel. Hopefully, this article will help you to solve your issue.
    Regards
    Exceldemy Team

    • Hi,
      in my case the code not work.
      When i have a string with two consecutive zeros the code generate a empty character…

      • Reply Lutfor Rahman Shimanto
        Lutfor Rahman Shimanto Dec 26, 2023 at 9:57 AM

        Hello GSFRAGARO

        Thanks for sharing your problem. You have encountered an issue like you have a string with two consecutive zeros, but the code generates an empty character. In Our machine, the code and font work perfectly fine if we have a string with two consecutive zeros. See the following Image,

        The possible reason for the issue you encountered could be not using the Code 128 font. Maybe you are using some other barcode font. So, please ensure you are using the intended font. Good luck.

        Regards
        Lutfor Rahman Shimanto
        ExcelDemy

  29. HELLO! Thank you for sharing, it helps me a lot.
    I’m seeking assistance with a problem.
    I download the practice xlsm above, and I review the excel settings and VBA code make sure setting correctly.
    Then, I scan the barcode in the book with my scanning gun, and it works.
    But when I press enter button on the cell D5 with the formula, the barcode changed. It’s different from the original one and can’t be scanned.
    I try other numbers and fail again. Is it the problem of my code128 font (alreday installed code 128 regular font).
    If you know how to solve it, could you help?
    Looking forward for your reply.

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Dec 26, 2023 at 11:07 AM

      Hello ALEX

      Thanks for your nice words. Your appreciation means a lot to us.

      You are facing a difficulty like when you press the enter button on cell D5 with the formula, the barcode changes. It’s different from the original one and can’t be scanned. Providing the ultimate solution without properly glancing at your problem is very hard. However, I am giving you some ideas that might be helpful.

      Suggestions: Your issue concerns the font encoding or handling of the Code 128 font in Excel. Make sure that the Code 128 font is installed correctly on your system. Check the scanner manual for any specific settings related to Code 128 barcodes. Format the cell as Text when using the Code 128 barcode font with a custom VBA function to ensure accurate content and avoid unintended changes by Excel formatting rules.

      You can also share your problem with more details in the ExcelDemy Forum. Good luck.

      Regards
      Lutfor Rahman Shimanto
      ExcelDemy

  30. Hi,
    This is great and was working as expected in generating scannable 128 barcodes, but I ran into an issue with simple numbers greater than 999.

    If I tried 1,000 2,000 3,000 I was getting invalid barcodes. The barcodes all have large gaps in middle. Any ideas?

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Jan 18, 2024 at 1:24 PM

      Hello Jon Jacobson

      Thanks for your nice words. The code and font work perfectly fine in our machine for numbers greater than 999. See the following Image,

      You can test the barcodes shown in the image using your barcode gun. On our end, we have found the original text accurately.

      It is impossible to thoroughly observe your problem remotely and not glance at your machine. Make sure you have installed the Code 128 barcode font properly. You can check the scanner manual for specific settings related to Code 128 barcodes.

      If you still face this type of problem, you can share the issue within the ExcelDemy Forum with details of your machine. Stay blessed.

      Regards
      Lutfor Rahman Shimanto
      Excel & VBA Developer
      ExcelDemy

  31. ITS KARAN HERE FROM DUBAI WE ARE UNABLE TO USE THE FILE IN WINDOWS 11, KINDLY HELP US

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Jan 18, 2024 at 3:11 PM

      Hello Karan

      Thanks for sharing your problem. The practice workbook attached to the article should be usable in Windows 11 OS.

      As the Excel file is macro-enabled, check your Excel security settings: Go to Excel Options > Trust Center > Trust Center Settings > Macro Settings, and enable macros if they are disabled.

      Besides, ensure that the Code 128 font is correctly installed on your system.

      Hopefully, these actions will resolve your issue. Good luck.

      Regards
      Lutfor Rahman Shimanto
      Excel & VBA Developer
      ExcelDemy

  32. I encountered a problem with the VBA example file. In example file the cell C5 “A00M23233” is supposed to be transcoded to a string “ÌA00M2Ç@A8Δ which is in Code 128 font at cell D5. When I rerun the function in D5 the string change to “?A00M2?@AZ?”, which make the barcode differ from original and cannot be read. Pressing undo doesn’t help. Seems the VBA code cannot run properly on my system.

    My computer running windows 7 with office 2013.

    • Reply Lutfor Rahman Shimanto
      Lutfor Rahman Shimanto Feb 7, 2024 at 2:09 PM

      Hello FRANKY LAM

      Thanks for visiting our blog and sharing your problem. Based on your problem, it seems the VBA User-defined function named Code128 is not recalculating on your system.

      So, you can try forced fully recalculating using the CalculateFull property of the Excel Application.

      Follow these steps: Right-click on the sheet name tab >> Click on View Code >> Paste the following code in the sheet module and Save:

      
      Private Sub Worksheet_Change(ByVal Target As Range)
          Call ForcedReCalculation
      End Sub
      
      Sub ForcedReCalculation()
          Application.CalculateFull
      End Sub
      

      Hopefully, the idea will help you good luck.

      Regards
      Lutfor Rahman Shimanto
      Excel & VBA Developer
      ExcelDemy

  33. Hello, I encountered the same issue as Franky Lam, I using your data set fuke as a test, however, it shows ?A00M2?@AZ? instead of “ÌA00M2Ç@A8Δ.

    I guess for this reason the scanner could not read after it turns to code128. I tried the suggestion of using “CalculateFull”, but still not working. Any advice?
    I am using microsoft 365.

    Thank you for your help in advance.

    • Reply Mahfuza Anika Era
      Mahfuza Anika Era Feb 28, 2024 at 2:47 PM

      Hi P.Chan,

      The scanner cannot scan or scan wrong information as the text the code is generating is wrong in your case. I have tried the code and it is working properly.

      As for why the code isn’t running properly in your system is a bit complicated to trace. For starters, try troubleshooting and retry the code again.

      Regards
      Niloy
      ExcelDemy

  34. Works perfectly !! Thank you !

  35. Am I correct in saying VB script this is Code 128B
    What would I need to do to make this Code 128A?

    • 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

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo