+ Reply to Thread
Results 1 to 16 of 16

Macro to find text and display entire row in message box

  1. #1
    Registered User
    Join Date
    09-27-2012
    Location
    Töreboda, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    11

    Macro to find text and display entire row in message box

    Hi all,

    I've a workbook with a few sheets where column A=old partnr, B=old name, C=old E-nr, D=comment, the columns E, F, G and H is the same but for the new part.

    I want to add a search box where I can search for a string, could be the name, partnr, new or old, or what ever, and if it's found I want to display the entire row in a message box, if the search string is match more than once, I want the message box to display all matches.

    I hope it make sence, I appreciate any help.

    Thanks

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macro to find text and display entire row in message box

    Try this :
    Please Login or Register  to view this content.
    Please click the * below if this helps
    Please click the * below if this helps

  3. #3
    Registered User
    Join Date
    09-27-2012
    Location
    Töreboda, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to find text and display entire row in message box

    Thanks a lot Jasper

    It work's perfectly, but... is there any way to split the result in two rows in the message box? i.e. first line= columns A, B, C, D second line=columns E, F, G, H and maybe a empty line after that, just to separate if the search has hit more than one match.

    Thanks

    Ooops, sorry Jasper,

    I had only tested it on one sheet, when I tried to search for a part in a different sheet I got an error, is there a way to modify the macro so it can search in all sheets, and if it don't find a match it will display some kind of message.

    Thanks again.
    Last edited by n00ne; 07-03-2013 at 08:13 AM.

  4. #4
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macro to find text and display entire row in message box

    plz upload sample workbook....

  5. #5
    Registered User
    Join Date
    09-27-2012
    Location
    Töreboda, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to find text and display entire row in message box

    Here is the workbook, it's in Swedish but I suppose it's okej anyway :-)
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-23-2014
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Macro to find text and display entire row in message box

    Hi, The code helped me a lot on one of my application. Thanks! But the code results an error if there is no cell contains the input string. Can the code be modified to include a msgbox saying the string could not be found?

  7. #7
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Macro to find text and display entire row in message box

    Hi there,

    I had a bit spare time and made a small search form for you.
    it iterates through all worksheets and searches the used rows for the given data
    Attention it only searches the first 9 columns cause of the restriction for unbound listboxes. (for your data it should be fine anyway only 8 cols max)
    you can also search for substrings doesnt need 100% match.
    The Result is shown in a listbox together with the Adress (worksheet|Adress)

    when you double click an entry in the listbox it will jump to the sheet and row where that entry was found.

    Hope this helps you
    have a nice day
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    09-27-2012
    Location
    Töreboda, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Macro to find text and display entire row in message box

    Thanks a lot LordLoki, it works great.

    Thanks again and have a nice day

  9. #9
    Registered User
    Join Date
    06-20-2015
    Location
    singapore
    MS-Off Ver
    2013
    Posts
    13

    Re: Macro to find text and display entire row in message box

    Quote Originally Posted by LordLoki View Post
    Hi there,

    I had a bit spare time and made a small search form for you.
    it iterates through all worksheets and searches the used rows for the given data
    Attention it only searches the first 9 columns cause of the restriction for unbound listboxes. (for your data it should be fine anyway only 8 cols max)
    you can also search for substrings doesnt need 100% match.
    The Result is shown in a listbox together with the Adress (worksheet|Adress)

    when you double click an entry in the listbox it will jump to the sheet and row where that entry was found.

    Hope this helps you
    have a nice day

    Hi LordLoki,

    Is it possible to display only selected columns (example "Type" and "MPG") and not the entire row in the listbox?
    Also, if i have more than 10 columns in the worksheets, what are the changes needed in the code.

    Still learning...

    Thanks in advance.

  10. #10
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Macro to find text and display entire row in message box

    Hi cklee,

    Pretty much everything is possible with VBA just have to find the right workaround :D
    When you stick with the 9 columns in the Listbox it is not that big of a change. I would create an array with the column numbers that you want to show and then lopp through that array instead of the whole row.
    When you want more then the 9 Columns it gets a little bit more difficult cause for no good reason you can only fill the Listbox with 10 columns if you not use rowsource to fill it.
    So if you want more then 9 columns you need to create a result Sheet or put the Data in an Array.
    I would go with the Result sheet cause the Array you need would be multidimensional and thats always a mess.

    Attached you find an example with settings for the rows to show. Remember you can still only use 9 columns to show cause number 10 is reserved to store the adress so that doubleklick jump to entry is working.
    I also moved the adress to the last column and dont show it cause it looks better

    you can set your searchrange and the columns you want to be shown in this code part

    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    06-20-2015
    Location
    singapore
    MS-Off Ver
    2013
    Posts
    13

    Re: Macro to find text and display entire row in message box

    Quote Originally Posted by LordLoki View Post
    Hi cklee,

    Pretty much everything is possible with VBA just have to find the right workaround :D
    When you stick with the 9 columns in the Listbox it is not that big of a change. I would create an array with the column numbers that you want to show and then lopp through that array instead of the whole row.
    When you want more then the 9 Columns it gets a little bit more difficult cause for no good reason you can only fill the Listbox with 10 columns if you not use rowsource to fill it.
    So if you want more then 9 columns you need to create a result Sheet or put the Data in an Array.
    I would go with the Result sheet cause the Array you need would be multidimensional and thats always a mess.

    Attached you find an example with settings for the rows to show. Remember you can still only use 9 columns to show cause number 10 is reserved to store the adress so that doubleklick jump to entry is working.
    I also moved the adress to the last column and dont show it cause it looks better

    you can set your searchrange and the columns you want to be shown in this code part

    Please Login or Register  to view this content.


    Thanks LordLoki!

    Eh, I'm lost here :P
    This is my second attempt at VBA (expanding on what I've picked up here in the forum).
    I was going through the forum and found this thread that I think I could actually use it. But too complicated for me.

    In my worksheets, I should have more than 10 columns, 1/2 of which are just for reference purposes. So actual search range will be limited to about 6 - 7 columns which I would search and have them displayed.

    About the address you moved, that's great! I was trying to have them removed! lol.

    Now I have to figure out how it works..

    Best regards.

  12. #12
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Macro to find text and display entire row in message box

    so if you only have 6-7 rows to display you are good to go with the actual solution.
    The Search Range can be as many columns as you like.
    Only the Result that is shown in the listbox is limited to 9 or 10 if you dont need the double click jump to entry feature.

    To make it work you just change the sc to the column where your search should start and lc to the column in which your search ends
    For example Look in columns B to Z would be
    sc = 2
    lc = 26
    And from all this columns you want to display C,D,G then you change resultcols = Array(1, 2, 3, 4, 5, 6, 7, 8, 9) to
    resultcols = Array(3,4,7)

    If you have more questions feel free to ask

  13. #13
    Registered User
    Join Date
    06-20-2015
    Location
    singapore
    MS-Off Ver
    2013
    Posts
    13

    Re: Macro to find text and display entire row in message box

    Thanks LordLoki,

    I managed use your solution and make it to work for me, the double click jump feature proved to be quite useful. Wunderbar!
    Just wondering if I could add in the header into the textbox and set the column width within the textbox.

    Oh yes, i noticed your search function is faster. There is a lag when I clicked on search, the result is not immediate.

    By the way, which part of Switzerland are you from???

    Thanks
    Last edited by cklee; 07-09-2015 at 12:52 PM.

  14. #14
    Forum Contributor
    Join Date
    04-20-2015
    Location
    Switzerland
    MS-Off Ver
    2010
    Posts
    312

    Re: Macro to find text and display entire row in message box

    With this method you have to use labels for the header. Cause labels only work when you use the rowsource to fill the listbox. Columnwidth is easy will post that later I'm not at the computer at the moment.

    I'm living in Thurgau and work in Zürich

    Gesendet von meinem HTC One mit Tapatalk

  15. #15
    Registered User
    Join Date
    06-20-2015
    Location
    singapore
    MS-Off Ver
    2013
    Posts
    13

    Re: Macro to find text and display entire row in message box

    Ah Zurich. I used to visit Zug for work in the past.
    Really missed those trips to Switzerland.

  16. #16
    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
    52,926

    Re: Macro to find text and display entire row in message box

    Guys, please take a few minutes to read through the forum rules....
    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    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

+ 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