+ Reply to Thread
Results 1 to 9 of 9

Return corresponding values of one search criteria using VBA

  1. #1
    Forum Contributor
    Join Date
    09-02-2013
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    368

    Return corresponding values of one search criteria using VBA

    Hello,
    I'm having a difficulty to resolve what I will consider a simple VBA problem.
    In one worksheet ("Find result"), I have a cell ("B2") called member iD which consists of the column A of the data on another worksheet ("Membership").
    The purpose of the exercise is to search if the cell ("B2") is on the column A of ("Membership") worksheet and list all the corresponding results (Date and Product) on the column ("D" & "E") of the ("Find result") worksheet. [as per the example attached]
    So I assume the VBA code will search first the corresponding value and then copy all matching criteria and then paste along the row until there is no more. [working as a loop]

    I have started the following code but because the result needs to be going to another worksheet, i.e. ("Find result"), I don't get anything.

    Please Login or Register  to view this content.
    Clearly it is not perfect and something is missing but cannot figure it out. I would appreciate your suggestion, help or advice.

    Thank you
    Last edited by rakotonirinas; 04-24-2018 at 11:54 AM. Reason: SOLVED

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Return corresponding values of one search criteria using VBA

    Hi rakotonirinas,

    A few notes about the code you posted.

    1. Does it run through and just output nothing? It seems to me that you should encounter a problem because "Member ID" should not work as a variable... since it has a space in it... maybe add an underscore? (Member_ID).
    2. Your code starts by clearing B2... (Sheets("Find result").Range("B2").ClearContents), but then next line then makes Member_ID the value of that cell... so isn't Member_ID blank at that stage?
    3. The code pastes in D6, so every match would over-write the previous one.
    4. No workbook was actually attached... to attach a workbook scroll down and on the right you will find "Go Advanced" - click that, then scroll down and find "Manage Attachments", you will have a screen where you can select a file, and then upload it.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    09-02-2013
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    368

    Re: Return corresponding values of one search criteria using VBA

    Hi Arkadi,

    Thank you for your help.

    I've deleted the part of clearing the content of the cell B2. So what should be the formula that would list all possible result as per the attachment for instance?

    I've attached the spreadsheet for more explanation.

    Many thanks again.
    Attached Files Attached Files

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Return corresponding values of one search criteria using VBA

    One of your problems is also that the active sheet is Find Result, and when you copy the ranges without specifying the source sheet, it assumes the activesheet.

    See if this works for you (I also changed copy to just assigning values, so that the formatting would stay the way you format your output sheet):

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    09-02-2013
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    368

    Re: Return corresponding values of one search criteria using VBA

    Arkadi. Thank you very much. It works like a charm and it looks so simple after you tweaked it. Much appreciated.

  6. #6
    Forum Contributor
    Join Date
    09-02-2013
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    368

    Re: Return corresponding values of one search criteria using VBA

    For the formula provided by Arkadi above, is there any possibility to Auto-Wrap the cell where the result will go, i.e., in Find result sheet From Range E6 to E20. My current issue is that when the value from the Membership worksheet is pasted, I have to wrap manually to be able to see the whole content of some results. I would like to auto-wrap or auto-fit if it is possible so the whole content of the result will be directly visible.

    Thank you.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Return corresponding values of one search criteria using VBA

    My pleasure, glad I could help Thanks for the rep and marking the thread as solved!

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Return corresponding values of one search criteria using VBA

    Please Login or Register  to view this content.
    Last edited by Arkadi; 04-24-2018 at 01:01 PM.

  9. #9
    Forum Contributor
    Join Date
    09-02-2013
    Location
    London
    MS-Off Ver
    Excel 2019
    Posts
    368

    Thumbs up Re: Return corresponding values of one search criteria using VBA

    Once again, thank you Arkadi. I cannot thank you enough.
    Clearly I would never have guessed to write it this way
    Please Login or Register  to view this content.
    Muchas gracias!

+ 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. [SOLVED] Return Multiple values from a column with index and match, and search criteria
    By marcusduton in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 02-15-2015, 09:59 PM
  2. [SOLVED] Return multiple values using three search criteria
    By knightjob in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-09-2014, 03:25 PM
  3. Replies: 3
    Last Post: 12-13-2013, 06:23 AM
  4. Replies: 7
    Last Post: 08-04-2013, 03:41 PM
  5. Replies: 4
    Last Post: 12-21-2012, 10:35 AM
  6. [SOLVED] How to return multiple values that match a single search criteria?
    By JSallen in forum Excel General
    Replies: 4
    Last Post: 11-28-2012, 11:49 AM

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