+ Reply to Thread
Results 1 to 14 of 14

Matching up two lists of codes using VBA

  1. #1
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Matching up two lists of codes using VBA

    I am trying to avoid using formulas here so need a VBA solution.

    I am trying to match up two lists of product codes and their respective bar codes.

    (I have added an example workbook to explain the problem)

    In Sheet1 I have the following:

    Product Codes...........Bar Codes
    PCode 1.....................Bar code 1
    PCode 2.....................Bar code 2
    PCode 3.....................Bar code 3
    PCode 4.....................Bar code 4

    And in sheet 2 i have the same product codes except they have a four character suffix added on - and different bar codes in column 2.

    So where a product code on sheet 1 would be 'product-code-1' on sheet 2 it would be 'product-code-1-abc'

    I am trying to write a VBA code that will ignore the last four characters of the product codes on sheet2 and match them to those sheet1, and where there is a match copy the relevant barcode from sheet2 to third column on Sheet1. So it would be

    Codes.................Bar Codes..........Bar Codes from Sheet2
    PCode 1.....................Bar code 1............Bar code 1
    PCode 2.....................Bar code 2............Bar code 2
    PCode 3.....................Bar code 3............Bar code 3
    PCode 4.....................Bar code 4............Bar code 4

    I have been struggling with this for a few days so any help anyway can offer would be much appreciated
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Matching up two lists of codes using VBA

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Matching up two lists of codes using VBA

    just for fun an approach using a recordset

    Please Login or Register  to view this content.
    If you like my contribution click the star icon!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,025

    Re: Matching up two lists of codes using VBA

    One way:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Sorry, missed the bit about code not formula ...

    Please Login or Register  to view this content.

    Regards, TMS
    Last edited by TMS; 10-01-2013 at 07:36 AM. Reason: Added VBA code
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Matching up two lists of codes using VBA

    Fantastic! Thanks Andy that works perfectly!

    I didn't know you could use formulas in the actual coding in that way.

    Anyway cheers!

  6. #6
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Matching up two lists of codes using VBA

    Quote Originally Posted by OllieB View Post
    just for fun an approach using a recordset
    Thanks Ollie that works too. Is a recordset similar to a scripting dictionary then?

  7. #7
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Matching up two lists of codes using VBA

    Cheers dude, yeah I can do index match it was more the VBA. Your code works perfectly too though so thanks for your help

    Quote Originally Posted by TMShucks View Post

    Sorry, missed the bit about code not formula ...

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,025

    Re: Matching up two lists of codes using VBA

    You're welcome. Thanks for the rep.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Matching up two lists of codes using VBA

    You might use:

    Please Login or Register  to view this content.



  10. #10
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Matching up two lists of codes using VBA

    Quote Originally Posted by strud View Post
    Thanks Ollie that works too. Is a recordset similar to a scripting dictionary then?
    Actually a recordset is like a database table or a query over multiple database tables. In this case I treated Sheet1 and Sheet2 as tables and created a recordset consisting of the output of a join operation. As you were only interested in one single output column this approach is a bit silly. But if you had many rows and columns then building and dumping the recordset contents is a very fast and simple method to use.

  11. #11
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Matching up two lists of codes using VBA

    I have just tried editing the formula in your code TMshucks:

    Quote Originally Posted by TMShucks View Post

    Please Login or Register  to view this content.

    I tried to add IFERROR so it reads:

    Please Login or Register  to view this content.
    it works just as a formula when I enter it that way ( i did it to test it) so why not in the code?

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Matching up two lists of codes using VBA

    you need to double up on the quotes used for the IFERROR failure section.

    Please Login or Register  to view this content.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Matching up two lists of codes using VBA

    @OllieB

    Did you consider ?

    Please Login or Register  to view this content.

  14. #14
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Matching up two lists of codes using VBA

    Quote Originally Posted by snb View Post
    @OllieB

    Did you consider ?

    Please Login or Register  to view this content.
    Dear snb,

    Many thanks for the excellent suggestion.

    I am sure it works, but I am somebody who personally likes to write out (and document) code in full because I (again) personally believe that that the resulting code would be easier to expand, maintain and debug. Also given the fact that the some of the OPs do not have much experience with VBA, I believe it is of more value to them to write of lot of explanations and to take things step by step.

    I guess in the end it is just a personal preference, but thanks again for the suggestion

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Matching post codes to areas
    By radmar1985 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-08-2013, 09:38 AM
  2. Matching Zip Codes to Geographic Region
    By dforte in forum Excel General
    Replies: 3
    Last Post: 10-03-2011, 05:48 PM
  3. Matching codes and data into one row
    By darell in forum Excel General
    Replies: 1
    Last Post: 08-01-2011, 02:23 AM
  4. Codes for Matching HSCodes
    By RAparente in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-02-2008, 01:55 AM
  5. matching codes between 2 item codes
    By JChan in forum Excel General
    Replies: 1
    Last Post: 10-15-2005, 01:05 AM

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