+ Reply to Thread
Results 1 to 12 of 12

Search for worksheet by name or cell value

  1. #1
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Smile Search for worksheet by name or cell value

    I'm using the code below to search for and display any sheet in a workbook, this works fine but I would like to be able to use the same input box to search on a value in cell A1.
    So if the user enters the sheet name OR the value of cell A1 the appropriate sheet will be displayed.
    I've attached the workbook in the hope that someone can help me out with this.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Mayweed; 03-17-2011 at 09:32 PM. Reason: Forgot to attach workbook

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

    Re: Search for worksheet by name or cell value - WB Attached

    Why don't you use Excel's inbuilt facility:
    Attached Images Attached Images



  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for worksheet by name or cell value - WB Attached

    Hello mayweed,

    Add this macro to your workbook.

    Worksheet Change Event Macro
    Please Login or Register  to view this content.

    How to Save a Worksheet Event Macro
    1. Copy the macro using CTRL+C keys.
    2. Open your Workbook and Right Click on the Worksheet's Name Tab for the Worksheet the macro will run on.
    3. Left Click on View Code in the pop up menu.
    4. Paste the macro code using CTRL+V
    5. Make any custom changes to the macro if needed at this time.
    6. Save the macro in your Workbook using CTRL+S
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Search for worksheet by name or cell value - WB Attached

    Thanks Leith but I cant get that macro to work at all, must something I'm doing wrong?

    Just to be clear... the macro should open an input box so that the user can enter a sheet name OR a string.

    If the string matches the value of any A1 cell of any sheet in the workbook, that sheet is displayed.
    OR
    If the string matches the name (on the tab) of any sheet in the workbook, that sheet is displayed.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for worksheet by name or cell value - WB Attached

    Hello Mayweed,

    Thanks for the updated info. I now understand what you want and that explains why the macro doesn't work. The macro needs to be rewritten.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for worksheet by name or cell value - WB Attached

    Hello Mayweed,

    I think this macro will do what you want. Try it and let me know.
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Search for worksheet by name or cell value - WB Attached

    Thanks Lieth, I tried your code and it selects the sheet by name but not by A1 cell value.
    I've attached a workbook to demonstrate.
    If the user enters the word "Blue", sheet 2 should display
    If the user enters "1234", sheet 1 should display
    Attached Files Attached Files

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for worksheet by name or cell value - WB Attached

    Hello Mayweed,

    You have no sheets named "1234", "Blue". "Orange", etc. The sheet names are "Sheet1", "Sheet2", "Sheet3".

  9. #9
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Search for worksheet by name or cell value - WB Attached

    My original post/attachment had code that searched and displayed the sheets by tab name, I would like to also be able to enter a string into the input box as follows.

    Locate and display a sheet based on users 'input box' value

    Method 1. Locate a sheet by entering the sheet name (This is working ok.)

    Method 2. Locate a sheet by entering a string (eg. "Orange" or "1234")
    Loop through all sheets in the workbook looking in cell A1 for the string (in this case "Orange")
    Last edited by Mayweed; 03-17-2011 at 09:03 PM. Reason: Add information to clarify my question

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search for worksheet by name or cell value

    Hello Mayweed,

    This ignores the tab names and uses only the input name or values in "A1".
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Search for worksheet by name or cell value

    Brilliant! it works just fine but would you combine the two so that I can enter the sheet name OR the string into the same input box?

  12. #12
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Smile Re: Search for worksheet by name or cell value

    Oops! You've already done that.
    Thanks for your help Leith, your code works perfectly.

+ 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