Figuring out a UPC check digit
A UPC barcode may look like a 12-digit number. It's really an 11-digit number with a check digit. The number in the lower right corner of a UPC version bar code is a check digit that's based on the previous 11 digits.
The first 11 digits in a UPC barcode are one's assigned company prefix and a product number. The check digit insures that a barcode was printed correctly and scanned without any errors.
A UPC check digit is calculated by adding up the odd digits, multipling the subtotal by three, adding that to the sum of the even digits, and then figuring out what single digit number raises it to the next multiple of ten. Confused?
This Excel spreadsheet calculates UPC check digits for you. There are Excel 2003 and Excel 2007 versions with both macros and User Defined Functions. 11 digits in, 12 digits out. Company prefix and product number and get the checkdigit.
Azalea Software's company prefix assigned by GS1 is "692771". This means we can use any 5 digit number from "00000" to "99999" for our individual barcodes. For our example I'm going to use my childhood ZIP code, "44120".
For a UPC bar code of "69277144120":
(6 + 2 + 7 + 4 + 1 + 0) = 20
20 x 3 = 60
9 + 7 + 1 + 4 + 2 = 23
60 + 23 = 83
83 + 7 = 90, the next multiple of 10
Here's how to calculate the UPC check digit in Visual Basic for Applications:
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
At some point you may need to print a UPC barcode. May I suggest UPCTools. UPCTools is a set of UPC barcode fonts for Windows & the Macintosh which comes with a library of sample code to calculate the UPC check digit for you.

