+ Reply to Thread
Results 1 to 15 of 15

Macro required to find matches and return value in the next column.

  1. #1
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Macro required to find matches and return value in the next column.

    Hi All,

    I have attached a worksheet for your reference. I am a new starter in the world of data validation. My task includes to make sure that I only pay for jobs which are original and not invoiced to us in the past. In order to solve duplication issue, I want to compare two worsheets, sheet 1 has the jobs invoiced to us for the current month and sheet two has got standard report from the past 12 months giving all jobs invoiced to us in the past 12 months. I need a VBA based macro to find all matching jobs which are in sheet 1 column A, in sheet2 column A and return the corresponding value in column B in sheet 2. Vlookup is a standard answer but because there can be a max of 1000 jobs invoiced to us in any given month, this is a tedious task. I did try to record macro to find these jobs and return the month by making the colour of cell red but every time i run that macro it has run time errors and it doesnt change the format of the months and gets stuck basically.

    I have attached the spreadsheet for help please.

    Any help woud be grearly appreciated.

    Thanks & Regards
    Manav
    Attached Files Attached Files

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,049

    Re: Macro required to find matches and return value in the next column.

    not sure why you need vba for this, i modified your formula so that you can copy it down as far as you need, and it will only show a result (0 or a value) if there is data in column A. see the attached

    Also, i noticed that your cell reference into A looked wrong. seems it is referencing the row below it? eg the formula in A2 was looking up the value in A3
    Attached Files Attached Files
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Macro required to find matches and return value in the next column.

    Hello (FDibbins)

    Thank you for your reply. Yes you are right regarding cell reference, actually I made the job numbers myself at random as I dont have access of the actual sheet at work. I was trying to explain the situation with a sample sheet also the reason I need vba is because I need to automate this at a touch of a button, basically if any new starter in the team it will save a lot of time in explaining formulas. I already have used a vba code(given below) to find duplicates within column A in itself and then assign that macro to a button. So just one press and all duplicates within column A would turn red. The same thing i want for the comparison as well through a vba macro.

    Thanks
    Manav

    Please Login or Register  to view this content.
    Last edited by arlu1201; 05-06-2012 at 06:44 AM. Reason: Corrected code tags

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,049

    Re: Macro required to find matches and return value in the next column.

    ok, what i was trying to do was let you copy my formula down a looooong way, and it would populate only if data was entered.

    my VBA is poor at best, is i will let 1 of the others pick this 1 up from here
    Also, you dont need the "" when typing in [code]

  5. #5
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Macro required to find matches and return value in the next column.

    If it must be VBA, this should do the trick. I admit that this is not the most efficient approach (which would be to analyze the entire thing in array and pass back...I'm working on that one).

    Please Login or Register  to view this content.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  6. #6
    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
    48,539

    Re: Macro required to find matches and return value in the next column.

    You don't need VBA for this.

    If you select all of column A on Sheet 2 and name it as S2NGM, you can use Conditional Formatting on the entries in Sheet 1.

    The CF formula would be: =COUNTIF(S2NGM,A2)>0 applied to cell A2 down.

    I note that your VLOOKUP is offset and not highlighting the correct entries. On row 11, you are referring to cell A12.

    Regards, TMS
    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


  7. #7
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Macro required to find matches and return value in the next column.

    OP, you agreed when you signed up not to cross-post without providing information as to having done so and a link. Mods, do as you will.

    http://www.ozgrid.com/forum/showthread.php?t=165089

  8. #8
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Macro required to find matches and return value in the next column.

    Dear Sir (AlvaroSiza):

    I was not expecting to get this results, it woked seamlessly, i/m just too excited to see the VBA power. No doubt programming always has an upper hand to excel formulas. I am so grateful to you, cant just express in words. Also I didnt quite understood your second post regarding some rules about posting, what was that about please.

    Billion thanks sir.


    Quote Originally Posted by AlvaroSiza View Post
    If it must be VBA, this should do the trick. I admit that this is not the most efficient approach (which would be to analyze the entire thing in array and pass back...I'm working on that one).
    Last edited by arlu1201; 05-08-2012 at 03:18 AM. Reason: Corrected quote tags.

  9. #9
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Macro required to find matches and return value in the next column.

    Thanks very much (TMShucks), I think I got it what i was looking for, your efforts are much appreciated. Many Regards Manav

  10. #10
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Macro required to find matches and return value in the next column.

    Quote Originally Posted by excelvba123 View Post
    Also I didnt quite understood your second post regarding some rules about posting, what was that about please.
    It is called cross-posting when you post in multiple forums looking for an answer to your question. This is frowned upon as the volunteers who take time to help you in one forum might be wasting their time as an answer might already be developed elsewhere. One of the rules that you agreed to when you signed up was to NOT cross-post. I found your identical query on OzGrid.

  11. #11
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Macro required to find matches and return value in the next column.

    You are absolutely right Sir, I really dont want to waste the precious time of senior members.

    I will make sure that I dont repeat that in future. Many Thanks & Regards for your esteemed help.








    Quote Originally Posted by AlvaroSiza View Post
    It is called cross-posting when you post in multiple forums looking for an answer to your question. This is frowned upon as the volunteers who take time to help you in one forum might be wasting their time as an answer might already be developed elsewhere. One of the rules that you agreed to when you signed up was to NOT cross-post. I found your identical query on OzGrid.

  12. #12
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Macro required to find matches and return value in the next column.

    Hi there,

    I have just started to use the code at work and it showing me the following error(Runtime error 9 Subscript out of range (Set ws2 = ActiveWorkbook.Sheets("Sheet2"), I have uploaded the actual file(Testfordups_Master)and I have assigned the second button (Find Duplicates since April) to the macro called compare, please advise further.

    Thanks
    Manav




    Quote Originally Posted by AlvaroSiza View Post
    It is called cross-posting when you post in multiple forums looking for an answer to your question. This is frowned upon as the volunteers who take time to help you in one forum might be wasting their time as an answer might already be developed elsewhere. One of the rules that you agreed to when you signed up was to NOT cross-post. I found your identical query on OzGrid.
    Attached Files Attached Files

  13. #13
    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
    48,539

    Re: Macro required to find matches and return value in the next column.

    You're code looks for "Sheet2", no spaces; the worksheet name is "Sheet 2", with a space.

    Change one or the other to match.


    Regards, TMS

  14. #14
    Registered User
    Join Date
    05-04-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Macro required to find matches and return value in the next column.

    Spot on, thanks a trillion. Many Thanks & Regards.




    Quote Originally Posted by TMShucks View Post
    You're code looks for "Sheet2", no spaces; the worksheet name is "Sheet 2", with a space.

    Change one or the other to match.


    Regards, TMS

  15. #15
    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
    48,539

    Re: Macro required to find matches and return value in the next column.

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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