+ Reply to Thread
Results 1 to 10 of 10

Macro to force format and check barcode check digit

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-16-2009
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2013
    Posts
    114

    Cool Macro to force format and check barcode check digit

    Hi,

    My company uses 4 types of barcodes 8, 12, 13, & 14 number barcodes for our products my problem is that I can't figure out how to force the barcode to format properly no matter how somebody enters it if they don't use spaces or put them in the wrong spots, I can't use custom formats because there is 4 different layouts

    8 digit should be "#### ####"
    12 digit should be "###### ######"
    13 digit should be "# ###### ######"
    14 digit should be "# ## ##### ######"

    these barcodes are in columns L, M, & N also right now 'm using a formula in another cell to verify the barcodes by calculating the check digit and comparing it to the check digit typed the formula i'm using is

    =IF(LEN(L2)=13,EXACT(--RIGHT(ABS((MOD((((MID(L2,1,1)+MID(L2,3,1)+MID(L2,5,1)+MID(L2,8,1)+MID(L2,10,1)+MID(L2,12,1))*3)+(MID(L2,2,1)+MID(L2,4,1)+MID(L2,6,1)+MID(L2,9,1)+MID(L2,11,1))),10)-10)),1),RIGHT(L2,1))
    
    ,IF(LEN(L2)=17,EXACT(--RIGHT(ABS((MOD((((MID(L2,1,1)+MID(L2,4,1)+MID(L2,7,1)+MID(L2,9,1)+MID(L2,12,1)+MID(L2,14,1)+MID(L2,16,1))*3)+(MID(L2,3,1)+MID(L2,6,1)+MID(L2,8,1)+MID(L2,10,1)+MID(L2,13,1)+MID(L2,15,1))),10)-10)),1),RIGHT(L2,1))
    
    ,IF(LEN(L2)=15,EXACT(--RIGHT(ABS((MOD((((MID(L2,3,1)+MID(L2,5,1)+MID(L2,7,1)+MID(L2,10,1)+MID(L2,12,1)+MID(L2,14,1))*3)+(MID(L2,1,1)+MID(L2,4,1)+MID(L2,6,1)+MID(L2,8,1)+MID(L2,11,1)+MID(L2,13,1))),10)-10)),1),RIGHT(L2,1))
    
    ,IF(LEN(L2)=9,EXACT((ROUNDUP(((MID(L2,1,1)+MID(L2,3,1)+MID(L2,6,1)+MID(L2,8,1))*3)+(MID(L2,2,1)+MID(L2,4,1)+MID(L2,7,1)),-1))-(((MID(L2,1,1)+MID(L2,3,1)+MID(L2,6,1)+MID(L2,8,1))*3)+(MID(L2,2,1)+MID(L2,4,1)+MID(L2,7,1))),RIGHT(L2,1))
    
    ,(LEN(L2)<4)))))
    I have that cell change to red if the check digits don't match, but i would prefer to have the cell with the upc change the fill to red if it is typed incorrectly but i don't know how to write the formula in vba. I'd want the macro to run whenever the cell contents are changed. any help would be much appreciated, i'm pretty new to VBA and I just can't figure this one out
    or find out how to do it on the net.

    Thanks,

    Chris
    Last edited by Code Flunkie; 12-02-2009 at 10:30 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to force format and check barcode check digit

    Maybe like this, assuming none of the barcodes have leading zeros. Code goes in the Sheet module:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r           As Range
        Dim cell        As Range
    
        Set r = Intersect(Target, Columns("L:M"))
        If r Is Nothing Then Exit Sub
    
        For Each cell In r
            Select Case r.Value
                Case 10000000 To 99999999
                    cell.NumberFormat = "#### ####"
                    cell.Interior.ColorIndex = xlColorIndexNone
                
                Case 100000000000# To 999999999999#
                    cell.NumberFormat = "###### ######"
                    cell.Interior.ColorIndex = xlColorIndexNone
                
                Case 1000000000000# To 9999999999999#
                    cell.NumberFormat = "# ###### ######"
                    cell.Interior.ColorIndex = xlColorIndexNone
                
                Case 10000000000000# To 99999999999999#
                    cell.NumberFormat = "# ## ##### ######"
                    cell.Interior.ColorIndex = xlColorIndexNone
                
                Case Else
                    cell.Interior.ColorIndex = 3
            End Select
        Next cell
    End Sub
    Adding Code to a Sheet module
    1. Copy the code from the post
    2. Right-click on the tab for the relevant sheet and select View Code. This opens the Visual Basic Editor (VBE) and shows the object module for the selected worksheet.
    3. Paste the code in the window
    4. Press Alt+Q to close the VBE and return to Excel

    It would be easy enough to add the check digit processing if you explain the algorithm (or provide a link) so I don't have to dissect your formula.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    07-16-2009
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2013
    Posts
    114

    Re: Macro to force format and check barcode check digit

    Unfortunately a lot of the barcodes have leading zeros

    Chris

  4. #4
    Forum Contributor
    Join Date
    07-16-2009
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2013
    Posts
    114

    Re: Macro to force format and check barcode check digit

    the algorithim for the barcode is the last digit is the check digit so it is calculated. so you strip the last digit. the odd digits are summed and then multiplied by 3, the even digits are summed and added to the total from the odd digits, and you take that number and whatever you would need to add to it to bring the total to a multiple of 10 thats your check digit. so lets say the odd numbers add up to 30 and the even numbers add to 21
    (30*3)+21 = 111 so the check digit would be 9

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to force format and check barcode check digit

    Maybe like this. I confess to minimal testing. Format columns L:M as text beforehand.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r           As Range
        Dim cell        As Range
        Dim s           As String
        Dim i           As Long
        Dim iSum As Long
    
        Set r = Intersect(Target, Columns("L:N"))
        If r Is Nothing Then Exit Sub
    
        On Error GoTo Oops
        Application.EnableEvents = False
        
        For Each cell In r
            With cell
                If VarType(.Value) = vbString Then
                    s = Replace(.Text, " ", "")
                End If
                
                If Not IsNumeric(s) Then
                    .Interior.ColorIndex = 3
                    Exit Sub
                End If
    
                Select Case Len(s)
                    Case 8
                        .Value = Format(Val(s), "0000 0000")
                        .Interior.ColorIndex = xlColorIndexNone
    
                    Case 12
                        .Value = Format(Val(s), "000000 000000")
                        .Interior.ColorIndex = xlColorIndexNone
    
                    Case 13
                        .Value = Format(Val(s), "0 000000 000000")
                        .Interior.ColorIndex = xlColorIndexNone
    
                    Case 14
                        .Value = Format(Val(s), "0 00 000000 000000")
                        .Interior.ColorIndex = xlColorIndexNone
    
                    Case Else
                        .Interior.ColorIndex = 3
                End Select
                
                If .Interior.ColorIndex = xlColorIndexNone Then
                    iSum = 0
                    For i = 1 To Len(s) - 1
                        iSum = iSum + Val(Mid(s, i, 1)) * IIf(i And 1, 3, 1)
                    Next i
                    iSum = WorksheetFunction.Ceiling(iSum, 10) Mod 10 - iSum
                    If iSum=10 Then iSum = 0
                    If Val(Right(s, 1)) <> iSum Then .Interior.ColorIndex = 3
                End If
            End With
            
        Next cell
    
    Oops:
        Application.EnableEvents = True
    End Sub
    Last edited by shg; 11-28-2009 at 01:06 AM.

  6. #6
    Forum Contributor
    Join Date
    07-16-2009
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2013
    Posts
    114

    Re: Macro to force format and check barcode check digit

    I tried it out and it is sort of working, except when you clear the contents of one of the target cells it fills the cell red then it doesn't work anymore till you close and reopen the file

    Chris

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to force format and check barcode check digit

    Post a workbook and explain how to break it.

  8. #8
    Forum Contributor
    Join Date
    07-16-2009
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2013
    Posts
    114

    Re: Macro to force format and check barcode check digit

    When i highlight any of the cells in L:N and hit the DEL key the cell turns red and the macro stops working. i'm using office 2007 by the way.

    Chris
    Attached Files Attached Files

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Macro to force format and check barcode check digit

    Bug in the code; try this:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r           As Range
        Dim cell        As Range
        Dim s           As String
        Dim i           As Long
        Dim iSum        As Long
    
        Set r = Intersect(Target, Columns("L:N"))
        If r Is Nothing Then Exit Sub
    
        On Error GoTo Oops
        Application.EnableEvents = False
    
        For Each cell In r
            With cell
                s = Replace(.Text, " ", "")
    
                If Not IsNumeric(s) Then
                    .Interior.ColorIndex = 3
                
                Else
                    Select Case Len(s)
                        Case 8
                            .Value = Format(Val(s), "0000 0000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case 12
                            .Value = Format(Val(s), "000000 000000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case 13
                            .Value = Format(Val(s), "0 000000 000000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case 14
                            .Value = Format(Val(s), "0 00 00000 000000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case Else
                            .Interior.ColorIndex = 3
                    End Select
    
                    If .Interior.ColorIndex = xlColorIndexNone Then
                        iSum = 0
                        For i = 1 To Len(s) - 1
                            iSum = iSum + Val(Mid(s, i, 1)) * IIf(i And 1, 3, 1)
                        Next iformatting in the code. 
                        iSum = WorksheetFunction.Ceiling(iSum, 10) - iSum
                        If Val(Right(s, 1)) <> iSum Then .Interior.ColorIndex = 3
                    End If
                End If
            End With
        Next cell
    
    Oops:
        Application.EnableEvents = True
    End Sub
    BTW, the conditional formatting you're using for alternate line shading will override the cell color applied in the code.

  10. #10
    Forum Contributor
    Join Date
    07-16-2009
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2013
    Posts
    114

    Re: Macro to force format and check barcode check digit

    Had a problem with this line but i figured it out
    "Next i formatting in the code."

    Code is working great, thanks a million

    Chris


    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r           As Range
        Dim cell        As Range
        Dim s           As String
        Dim i           As Long
        Dim iSum        As Long
    
        Set r = Intersect(Target, Columns("L:N"))
        If r Is Nothing Then Exit Sub
    
        On Error GoTo Oops
        Application.EnableEvents = False
    
        For Each cell In r
            With cell
                s = Replace(.Text, " ", "")
    
                If Not IsNumeric(s) Then
                    .Interior.ColorIndex = 0
                
                Else
                    Select Case Len(s)
                        Case 8
                            .Value = Format(Val(s), "0000 0000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case 12
                            .Value = Format(Val(s), "000000 000000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case 13
                            .Value = Format(Val(s), "0 000000 000000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case 14
                            .Value = Format(Val(s), "0 00 00000 000000")
                            .Interior.ColorIndex = xlColorIndexNone
    
                        Case Else
                            .Interior.ColorIndex = 3
                    End Select
    
                    If .Interior.ColorIndex = xlColorIndexNone Then
                        iSum = 0
                        For i = 1 To Len(s) - 1
                            iSum = iSum + Val(Mid(s, i, 1)) * IIf(i And 1, 3, 1)
                        Next i 'formatting in the code.
                        iSum = WorksheetFunction.Ceiling(iSum, 10) - iSum
                        If Val(Right(s, 1)) <> iSum Then .Interior.ColorIndex = 3
                    End If
                End If
            End With
        Next cell
    
    Oops:
        Application.EnableEvents = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1