+ Reply to Thread
Results 1 to 10 of 10

Macro to force format and check barcode check digit

  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

    Please Login or Register  to view this content.
    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
    2003, 2010
    Posts
    40,678

    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:
    Please Login or Register  to view this content.
    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
    2003, 2010
    Posts
    40,678

    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.

    Please Login or Register  to view this content.
    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
    2003, 2010
    Posts
    40,678

    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
    2003, 2010
    Posts
    40,678

    Re: Macro to force format and check barcode check digit

    Bug in the code; try this:
    Please Login or Register  to view this content.
    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


    Please Login or Register  to view this content.

+ 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