+ Reply to Thread
Results 1 to 30 of 30

Need macro that searches for a description based on a number from a drop down list

  1. #1
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Need macro that searches for a description based on a number from a drop down list

    Hello all,

    First time poster here and VERY new to VBA. I am requiring a macro that searches roughly 60 (yes 60 lol) worksheets in a workbook that returns the text from the correct fields of each worksheet into a summary sheet.
    For example:

    the # 101 may be in some, none, or all of the worksheets. The description in the cell beside it would be the reason it is there (ex 101 is a truck that broke down, reason being the diff pressure was high). I need a macro that when a truck # is picked from a drop down box, someone can press a search button, the macro then searches the worksheets for that truck #, and for each instance it finds that truck #, it then copies and puts the reason for breaking down into a summary.

    I realize I may be vague with some of this so forgive me. Thank you in advance for any help that may be provided.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Try this:

    Please Login or Register  to view this content.
    Directions for running the routine(s) just supplied

    Copy the code to the clipboard

    Open your Workbook and save your Workbook As Macro-Enabled

    Press ALT + F11 to open the Visual Basic Editor.

    Select “Module” from the Insert menu

    Type "Option Explicit" then paste the code into the white space on the right

    With the cursor between Sub and End Sub press F5 (F8 to Single Step)

    OR

    Press ALT + Q to close the code window.

    Press ALT + F8 then double click on the macro name
    Last edited by xladept; 01-05-2013 at 03:03 AM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Hi Peebly,

    Test the above code, and, if it works the way you want, then we can set up an event procedure or a button to call it into action - I would need to know the address of your drop down to do that and, if you could post an Example Spreadsheet the program would be easier to write and test - but try the interactive procedure above first and then let me know.

  4. #4
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    xladept,

    Sorry for the super late response in getting this to you. I just took one of the worksheets from within the workbook to use as a sample.

    On sheet 3 of the sample, you'll find the info I used for the code in the Summary worksheet. The listed date names are the names in the workbook (the date and letter associated change week by week, month by month).

    So what I am hoping to do is use a search button that scrubs the worksheets to search for and paste the truck #, and the reason it is down. If at all possible, to also paste what category it was under.

    ex. Condition Based Monitoring or Komatsu Breakdowns etc. (The headings for each category don't stay on the same line either due to adding and removing lines based on the volume during the day)

    Thanks again for any help you are able to provide
    Attached Files Attached Files

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    But did you test the code - and does it work the way you want???

  6. #6
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Yeah the code pulls up the truck entry field, I enter a truck # and it returns with "truck 120 reason is" and then posts a small reason. What I am hoping for it to do is pull all instances of that truck, and paste it, and the reason for breakdown, into a summary worksheet.
    Last edited by Peebly; 01-11-2013 at 05:11 PM.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    But did you test the code - and does it work the way you want???

    Well, anyway - here's a book from your sample - I used a Change event for the summary sheet, but we can still put in on a button if you still prefer it that way:

    Peebly.xlsm

  8. #8
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    I would prefer a button, for I won't be the main one using this, so it'd be nice to have a user friendly button that someone can just press if need be. I'm just currently trying to figure out how to work the macro you just gave me. As I said, I'm so new it's stupid .

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Ok - I'll put a button on the summary sheet. I gave instructions with my first post, but for the event procedure just enter the truck number in column A on the summary sheet in the book I sent you.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Here's a version with button - you can dress it up by going into design mode and changing some of the properties: Peebly.xlsm
    Last edited by xladept; 01-11-2013 at 09:20 PM.

  11. #11
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    You sir are a genius :D. Are you at all able to alter the code in order to pull the description of the breakdown?. Currently it is showing the location as to where it broke down. (column D), where I am looking for the reason which is located in F.

    And am I able to copy the code and just slap it into the entire workbook? Thanks again so much for your help and patience :D. Really is appreciated man

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Hi Peebly,

    Thanks for the compliment - this should get your reason:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    One final thing, I promise. Is there a way to add what date it is pulling from?. Also, I noticed that it is pulling the number (101 for ex) from the far right hand side as well (under the work order column). Is there a way to ensure that it is only pulling from the unit number field?.

    I just noticed that once in a while I get a runtime error '1004'. When I select to debug, It references this line...

    Do Until InStr(1, ws.Cells(r.Row - i, r.Column), "#"): i = i + 1: Loop
    Last edited by Peebly; 01-14-2013 at 05:03 PM.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    This will add the tab name before the count in Column 2:

    Please Login or Register  to view this content.
    The error will throw if there is no "Unit #" heading above the Truck number.

  15. #15
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Thanks again so much for the help. Really is greatly appreciated. I had a couple other questions regarding the information it pulls, but I have bugged you long enough :D. Thanks for everything xladept!

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    You're welcome! I'll be here!

  17. #17
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Okay, well I will post my issues I'm having. Don't feel obligated to reply cause you've already done enough:

    The reason for breakdown & category isn't being copied on every instance (even if there is one in the field).

    And if the number of a truck happens to be part of a work order number on the right hand side (ex: work order number is 30574113), it pulls a truck # instance if I was searching for truck 113 for ex.

  18. #18
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    107

    Re: Need macro that searches for a description based on a number from a drop down list

    Hello Peebly,

    Can you please attach the workbook with the working VBA. It will be very helpful.
    Thank you!

  19. #19
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Here is my entire december workbook with the VBA in it. I did notice as well xladept, that it seems to be falsely reporting some trucks as well. The easiest example is if you do a search for truck 219, it shows a description, etc for it. But shows that it is in the "Field Call Data" sheet, and not from the "Dec 31 I2 NS" sheet I located it in. So I am having that issue as well I noticed, as well as the previously posted ones.
    Attached Files Attached Files

  20. #20
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Hi Peebly,

    I altered some code and got:

    Dec 5 I2 DS-1
    Dec 5 L2 NS-1
    Dec 9 I2 NS-1
    Dec 9 I2 NS-2
    Dec 10 J2 NS-1
    Dec 11 J2 NS-1
    Dec 12 K2 DS-1
    Dec 14 L2 DS-1
    Dec 15 L2 DS-1
    Dec 15 K2 NS-1
    Dec 15 K2 NS-2
    Dec 16 I2 DS-1
    Dec 30 L2 NS-1
    Dec 31 I2 NS-1
    for truck 219 but no field call data?? - Here's the latest version:

    The reason for breakdown & category isn't being copied on every instance (even if there is one in the field).
    Please Login or Register  to view this content.
    Last edited by xladept; 01-15-2013 at 08:41 PM.

  21. #21
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Seems to have addressed the issue. On the rare occasion, is still seems to be putting the truck location in the reason column, and not pasting the reason. (219 being a good example again. for Dec 9, it shows "NSE" as the reason. That however is the location, yet the reason on that day wasn't pulled). Not sure if that is a quick fix or not.

    I also haven't tested a lot of trucks so far, but I seem to have a lockup issue when I tried pulling truck 108. It just seems to hang there after posting a couple results.

  22. #22
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    I think it's OK now:

    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Xladept, once again thank you for your patience and help in this matter. Everything looks perfect now, and I see no issues so far with locking up. I'll mark this thread as solved. Wish I had 9 stars to click for ya!. Thanks again.

  24. #24
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    You're welcome!

  25. #25
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Hi Peebly,

    Finest Kind!

    I added it to the end - you'll want to insert a column before the button:

    Please Login or Register  to view this content.
    What do you mean by input work order #??? Duhhh

    You need this new click routine also:

    Please Login or Register  to view this content.
    Now you can input the work order or the truck number as the last entry in column A.

    The truck number will be added in if you input the work order number.

    Let me know whether it's what you wanted.
    Last edited by xladept; 02-28-2013 at 03:01 PM.

  26. #26
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    Hi Peebly,

    I just kept playing with this now, if you double-click in any sheet but the summary, it will take you to the summary sheet. And I added a clear button. Peebly.xlsm
    Attached Files Attached Files
    Last edited by xladept; 02-28-2013 at 04:31 PM.

  27. #27
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Heya It works, but in the process also removes a few things. When I input the truck number, it pulls the work order and places it beside the truck number, as well as pulling the work order number into the seperate column I created.

    Also, it no longer pulls the Category anymore. Not sure if that's just happening on my end or not

  28. #28
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    No - the category thing was my fault, its fixed now - how do you want the printout to be - I realized that when we input just the work order that we weren't getting the truck number:

    Please Login or Register  to view this content.

  29. #29
    Registered User
    Join Date
    01-04-2013
    Location
    Alberta
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Need macro that searches for a description based on a number from a drop down list

    Well I just want it to show how it did previously, just adding the workorder column. OHHHHHH hey, is there a way to make each one LINK to the worksheet it's from?. So if I click the link, it goes to that entry??. That'd be sick

    Oh, I also noticed it pulls the WO and puts it beside the truck number as well as in it's own column. Is there a way to correct that so the work order only shows in the column next to Category?

  30. #30
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need macro that searches for a description based on a number from a drop down list

    OK - Here's the latest workbook - use the double-click to navigate to and from the summary sheet - to get a sheet you need to have the active cell in the same row as that sheet's data PeeblyX.xlsm

    BTW are you gong to do anything with the Equipment status sheets?

    You want sick??

    Here's one that will bring you back to the source row and truck number: PeeblyX.xlsm
    Last edited by xladept; 02-28-2013 at 07:52 PM.

+ 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