UPC version A check digit calculator in Excel Azalea_UPC_A_checkDigit Copyright 2009 Azalea Software, Inc. All rights reserved. www.azalea.com The macro in this spreadsheet calculates the UPC version A check digit for an 11-digit input. If you have a company prefix assigned by GS1 and a product number you can calculate the UPC check digit. 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). 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_UPC_A_checkDigit UPCTools prints UPC, ISBN-13, EAN, JAN, and ISSN 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/UPC Function Azalea_UPC_A_checkDigit(ByVal UPCnumber As String) As String ' UPCTools 16mar09 jwhiting ' Copyright 2009 Azalea Software, Inc. All rights reserved. www.azalea.com ' Calculating the UPC version A checkdigit in Excel 2003 ' Your input, UPCnumber, is a string consisting of an 11-digit number. ' That is, you don't need the check digit. With or without works. Dim checkDigitSubtotal As Integer ' a check digit throwaway ' Possible valid input includes 11-digits (no check digit), 12-digits (with check digit), or 14-digits (GTIN). Select Case Len(UPCnumber) Case 11 ' We're going to do the UPC check digit calculation because the input doesn't have it. Case 12 ' Let's strip your check digit and calculate ours from scratch. (Nothing personal.) UPCnumber = Left(UPCnumber, 11) Case 14 ' GTIN input, but we're going to strip the leading 2 and the last characters to extract the 11-digit input without check digit. UPCnumber = Mid(UPCnumber, 3, 11) Case Else ' Your error handling goes here... End Select ' Now we need to do the UPC A check digit calculation. ' Add up the numbers in the odd positions left to right. Multiple the result by 3. ' Add up the numbers in the even positions. Now add the first subtotal to the second. ' The UPC barcode check digit is the single digit number makes the total a multiple of 10. checkDigitSubtotal = (Val(Left(UPCnumber, 1))) + (Val(Mid(UPCnumber, 3, 1))) + (Val(Mid(UPCnumber, 5, 1))) + (Val(Mid(UPCnumber, 7, 1))) + (Val(Mid(UPCnumber, 9, 1))) + (Val(Right(UPCnumber, 1))) checkDigitSubtotal = (3 * checkDigitSubtotal) + (Val(Mid(UPCnumber, 2, 1))) + (Val(Mid(UPCnumber, 4, 1))) + (Val(Mid(UPCnumber, 6, 1))) + (Val(Mid(UPCnumber, 8, 1))) + (Val(Mid(UPCnumber, 10, 1))) Azalea_UPC_A_checkDigit = Right(Str(300 - checkDigitSubtotal), 1) ' Excel: B1=Azalea_UPC_A_checkDigit(A1) ' Or put another way, yourContainer.text=Azalea_UPC_A_checkDigit(yourInputString) End Function