+ Reply to Thread
Results 1 to 3 of 3

Help on simple hide/unhide but based on multiple cell values - please

  1. #1
    Registered User
    Join Date
    06-30-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    2

    Help on simple hide/unhide but based on multiple cell values - please

    Hi,

    I am relatively new to vba and learning on the job at the moment. Would be super grateful if someone can help on what will be simple I'm sure for your standards!

    I want to unhide certain rows 78:82, if the text 'disc' is inserted into any of the cell range below.

    If the text is not contained in any of the cell range, then I want the rows 78:82 to disappear.

    Now I think its the range that is causing the issues, but after spending 30 mins on it this morning I cannot find a solution.

    CODE
    Please Login or Register  to view this content.

    A massive thank you in advance for anyone able to help!
    Last edited by Exceler100; 07-05-2017 at 07:15 AM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Help on simple hide/unhide but based on multiple cell values - please

    I am going to throw two canned replies at you then explain the program.

    Canned Reply #1 - please use code tags in the future.
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

    Canned Reply #2
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    I can't say enough good about Option Explicit. Just do it and your coding life will be a lot easier.

    That being said. I think this line
    Please Login or Register  to view this content.
    means that if every cell in the range is "Disc." This is clearly not what you want.

    So I came up with the code below. The basic logic is to check to see if the cell changed is in the range (H68:J74) - this is the intersect part of the code. If the changed cell is outside the range then the intersection is nothing, hence the code. We don't want to do anything if the changed cell is not in the range.

    Then loop through the range to see if any of the cells are "Disc." If you find even one, set the flag to true. You can stop the loop here (Exit For) since any single occurrence is sufficient to hide the cells.

    Then check the flag and if it was "tripped" to TRUE (meaning a "Disc" was found), hide the rows, otherwise each cell passed the test and the flag remained false, so un-hide the rows.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by dflak; 06-30-2017 at 01:40 PM.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    06-30-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    2

    Re: Help on simple hide/unhide but based on multiple cell values - please

    Thank you very much for your help dflak, and also the explanation with is totally invaluable to learning more.

    I attempted to edit the post but when click it all of my text disappears! From searching the forum it seems this is a re-occurring issue. I will note this for future posts and hope to learn a lot more from all of you amazing people.

+ 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. Hide & Unhide Multiple Sheets based on different cell values
    By kspeese in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-19-2016, 02:46 AM
  2. Conditionally hide/unhide worksheets based on cell values
    By David O'Riley in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-05-2015, 09:47 AM
  3. Hide/Unhide Rows based on Changing Cell Values
    By mithusaini in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-14-2015, 10:51 AM
  4. simple macro to hide or unhide rows based on a cell value
    By Russ Fuquay in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-19-2013, 06:32 PM
  5. [SOLVED] Checkbox VBA - Hide/Unhide Rows and/or Columns based on cell values
    By gravy258 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-21-2013, 05:39 PM
  6. hide/unhide rows based on multiple cell values
    By saninmelbourne in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-20-2012, 06:45 PM
  7. Hide/Unhide Command Button based on multiple cell values
    By dagindi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-05-2011, 09:03 PM

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