Creating Code 128 code set C barcodes in Excel Azalea_Code_128_C Copyright 2009 Azalea Software, Inc. All rights reserved. www.azalea.com The macro in this spreadsheet creates Code 128 code set C barcodes when used with Azalea Software's C128Tools font package. Because this spreadsheet is built around a macro, you *must* enable macros for this spreadsheet to work! The .xls file is for Excel 2003 and the .xlsm is for Excel 2007. An alternative is to use a User Defined Function as an .xla (Excel 2003) or .xlam (Excel 2007). The macro accepts numeric string input that contain the Code 128 code set C character set. The macro calculates the check digit, adds the start and stop bars, and maps the output into the C128Tools character set. Format the returned value in a C128Tools font and a barcode will be displayed and printed. Press ALT-F11 to view the macro in the Visual Basic Editor. To add the macro to your own spreadsheet: Tools/Macro/Visual Basic Editor Insert/Module Paste in the macro code Close the Visual Basic Editor When you return to your spreadsheet, a new User Defined function is available: Azalea_Code_128_C C128Tools prints code set A, code set B, code set C, and GS1-128 barcodes. Available for Windows, OS X, Linux/UNIX, et al. Free sample code and free tech support. Buy online and download immediately. www.azalea.com/Code-128 Function Azalea_Code_128_C(ByVal yourData As String) As String ' C128Tools 16mar09 jwhiting ' Copyright 2009 Azalea Software, Inc. All rights reserved. www.azalea.com ' Creating a Code 128 code set C barcode in Excel 2003 ' Your input, yourData, is a string to be encoded as a Code 128 code set C symbol. ' yourData must be the Code 128 code set C character set. Input error checking is your responsibility. Dim temp As String ' a temporary placeholder Dim checkDigitSubtotal As Integer ' a check digit throwaway Dim i As Integer ' our loop counter Dim fontString As String ' a temporary placeholder Dim chunk As String ' loop chunk ' seed the variables fontString = Chr(183) ' code set C start glyph checkDigitSubtotal = 105 ' code set C start checkdigit value temp = yourData ' 2 character chunks ' pad odd length input with a leading zero goesHere PRN ' Calculate the Code 128 mod 103 check digit For i = 1 To Len(yourData) / 2 chunk = Left$(temp, 2) checkDigitSubtotal = checkDigitSubtotal + Val(chunk) * i Select Case Val(chunk) Case 0 fontString = fontString + Chr(206) Case 1 To 93 fontString = fontString & Chr(Val(chunk) + 32) Case Is > 93 fontString = fontString & Chr(Val(chunk) + 103) End Select temp = Right$(temp, Len(temp) - 2) Next i checkDigitSubtotal = checkDigitSubtotal Mod 103 ' Put together the final output string ' code set C start bar, the encoded string, check digit, stop bar Select Case checkDigitSubtotal Case 0 ' Mark Presley bug fixed 19jan09 Azalea_Code_128_C = fontString & Chr(206) & Chr(196) Case 1 To 93 Azalea_Code_128_C = fontString & Chr(checkDigitSubtotal + 32) & Chr(196) Case Is > 93 Azalea_Code_128_C = fontString & Chr(checkDigitSubtotal + 103) & Chr(196) End Select ' The output, Azalea_Code_128_C, needs to be formatted in one of Azalea Software's Code 128 fonts. ' Excel: B1=Azalea_Code_128_C(A1) ' Or put another way, yourContainer.text=Azalea_Code_128_C(yourInputString) End Function