+ Reply to Thread
Results 1 to 28 of 28

Conditionally Hide Columns based on cell result

  1. #1
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Conditionally Hide Columns based on cell result

    Hello. I am new to manipulating excel via VBA. I am trying to conditionally hide an entire coloumn based on a cell result in the given column. I have gotten only the below code to work.
    Please Login or Register  to view this content.
    I can get this to hide coloumn B as I want, but it only works for coloumn B (obviously). I want it to work for a range of coloumns from B to BA such that B3=0-->hide colB , C3=""-->display col C , D3=0-->hide colD , ... etc. up to coloumn BA

    I'm assuming the answer is as simple as nesting the above code in a while loop or something similar from B:BA, but I don't have the know-how to program such a macro.

    Thanks in Advance!
    Last edited by montego; 07-01-2010 at 01:04 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Conditionally Hiding Coloumns based on cell result

    This is one option
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hiding Coloumns based on cell result

    Hi Dave;
    Shouldn't this line
    Quote Originally Posted by davesexcel View Post
    Please Login or Register  to view this content.
    Be
    Please Login or Register  to view this content.


    I'm also curious. Mose people use Intersect() instead of Union(). Is there a reason you don't?
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  4. #4
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hiding Coloumns based on cell result

    Thank you both for your comments.

    I was also curious about the range being B1:BA100, as the cell I want to reference the columns to is the 3rd row only, with other rows containing other information.

    Dave: I tried copying/pasting your code and I couldn't get it to work. I tried changing the range as foxguy suggested, but no dice. This code is run after any event in Excel correct?

    Out of all the code I found the code in the original post is the only one I have had work so far. (Yes, I know, I suck at VBA).

    Some other codes I tried with no success...

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

  5. #5
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hiding Coloumns based on cell result

    This code is run after any event in Excel correct?
    Not all code is run after every event.
    1) Code has to be in the sheet's module to run after an event. (Right Mouse click the sheet's tab and select "View Code")
    2) The different macros run after different events.
    Worksheet_Change runs after you change any cell's value (or a macro changes a cell's value).
    Worksheet_SelectionChange runs after you change selection in the sheet.
    Etc.

    So it depends on when you want the macro to run. If you want it to constantly be checking to see if cells B3:BA3 have changed put it into Worksheet_SelectionChange, but if you only want to check if B3:BA3 has been changed then put into Worksheet_Change
    I'm pretty sure that everyone would recommend you put it into Worksheet_Change

  6. #6
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hiding Coloumns based on cell result

    Ok, Thanks.

    I'll probably eventually have it set so it only runs when the cells change, but for now I want to run it off of selection change to see if the code works.

    Again, Thanks for clearing that up for me.

  7. #7
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Talking Code WORKS

    Dave,

    Your Code Works! I'm pretty sure it is doing exactly what I want it to. I didn't think it was working because it was toggled based off when B3:BA3 Values change right? that's
    Please Login or Register  to view this content.
    I changed the code to...

    Please Login or Register  to view this content.
    in order to update with selection change (Thanks Foxguy for clearing that up!!) in order to test the code was working, AND IT WORKED!

    I'm still curious why the Range its referencing are rows 1:100, but, it seems to be working

    Any one care to elaborate on why the B1:B100 Range Works?

  8. #8
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Code WORKS

    Quote Originally Posted by montego View Post
    I'm still curious why the Range its referencing are rows 1:100, but, it seems to be working
    Any one care to elaborate on why the B1:B100 Range Works?
    You will discover that it hides the column when you change ANY cell in rows 1:100 to 0.
    If you want it to hide the column only when row 3 is changed to 0 then use B3:BA3

  9. #9
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Uhoh, problems

    I was messing around and noticed that alhtough the code worked the first time, when I scrolled over it hid every coloumn from B:BA, regardless of what was in row 3 of each coloumn. Deleted code in VBA editor, repasted it, scrolled through the sheet again and seems to be working.

    Overall it looks like the code works, but gets screwed up when I manually unhide hidden columns by right clicking and then unhide. Deleting the Code and Repasting seems to fix it.
    Last edited by montego; 07-01-2010 at 12:50 PM.

  10. #10
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Uhoh, problems

    Quote Originally Posted by montego View Post
    I was messing around and noticed that alhtough the code worked the first time, when I scrolled over it hid every coloumn from B:BA, regardless of what was in row 3 of each coloumn
    Without seeing your workbook, I would guess that the line
    Please Login or Register  to view this content.
    is not working right.

  11. #11
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Code WORKS

    Quote Originally Posted by foxguy View Post
    You will discover that it hides the column when you change ANY cell in rows 1:100 to 0.
    If you want it to hide the column only when row 3 is changed to 0 then use B3:BA3
    Hmm, Well where does it reference Row 3 to check against?

  12. #12
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Uhoh, problems

    Quote Originally Posted by foxguy View Post
    Without seeing your workbook, I would guess that the line
    Please Login or Register  to view this content.
    is not working right.
    Ok, This probably has something to it jumping out of the code after scrolling. I assume If it's just reference to cell value changes (as in the original code posted by Dave), It should Work...I'll keep you updated.

    Thank you Again!

  13. #13
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Code WORKS

    Quote Originally Posted by montego View Post
    Hmm, Well where does it reference Row 3 to check against?
    Please Login or Register  to view this content.
    Will hide the column when ANY cell in rows 1:100 changes to 0
    Please Login or Register  to view this content.
    Will hide the column only when row 3 changes to 0

  14. #14
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Code WORKS

    Hi montego;

    FYI: after a column is hidden, if you want to unhide it, just have a macro change the contents of the cell in row 3 to "" or any other value except 0. This will trigger Worksheet_Change again and unhide the column

  15. #15
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hiding Coloumns based on cell result

    AHHH OK!

    Things are making sense now! $B$3:$B$A$3 Is what I want and only referenced off those changing values.
    Please Login or Register  to view this content.

    Foxguy: Thank you for all your help and your extremely quick responses. I 'approved' your response on your first post, but I am unable to do it for the other posts because I'm a newb. Anything else I can do for you?

  16. #16
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hiding Coloumns based on cell result

    Quote Originally Posted by montego View Post
    Foxguy: Thank you for all your help and your extremely quick responses. I 'approved' your response on your first post, but I am unable to do it for the other posts because I'm a newb. Anything else I can do for you?
    Nope, that does it. I'm on my way out the door. If any thing else comes up, I'll be back in 4 hours.

  17. #17
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Code WORKS

    Quote Originally Posted by foxguy View Post
    Hi montego;

    FYI: after a column is hidden, if you want to unhide it, just have a macro change the contents of the cell in row 3 to "" or any other value except 0. This will trigger Worksheet_Change again and unhide the column
    So now I'm thinking I should have two buttons on the sheet. One would be "Show relevant Data"
    Please Login or Register  to view this content.
    and It would be linked to the Current code Dave poste, Something Like

    Please Login or Register  to view this content.
    Then the second Button would be "Show All Cells"
    Please Login or Register  to view this content.
    here is what I am thinking

    Please Login or Register  to view this content.
    Last edited by montego; 07-01-2010 at 02:30 PM.

  18. #18
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Code WORKS

    Hi montego;

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hide Columns based on cell result

    Foxguy: I really hate to ask you, but I need a tiny bit more help.

    The code You gave for the command button 2 works great. However, I am not able to get the 'hide coloumn' code to interface correctly. my code....

    Please Login or Register  to view this content.
    and I get error "Argument not optional"

    Thanks for all the help rendered and in advance for me bugging you one last time.

  20. #20
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hide Columns based on cell result

    Hi montego;

    You have the 2 macros mixed up. This is what you should have assigned to a button.
    Please Login or Register  to view this content.
    This is the code for Worksheet_Change with the name changed.
    Please Login or Register  to view this content.
    BTW the error you are getting is saying that the Macro is expecting a "Target", that you can't pass directly from a button.

  21. #21
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hide Columns based on cell result

    Hi, I think I might have confused You.

    I now have two buttons, as shown http://farm5.static.flickr.com/4096/...ebb7db94_b.jpg


    My code is...

    Please Login or Register  to view this content.

    The bottom half works, and displays all coloumns when clicked. I can't figure out how to get the top button to conditionally hide the cells though. THANKS AGAIN

  22. #22
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hide Columns based on cell result

    Hi montego;

    Please Login or Register  to view this content.
    This sub will never hide any columns. It needs a Target passed to it. The code works in Sub Worksheet_Change(ByVal Target as Range) because the cell that is being changed is passed to the sub as Target.
    I assume you get an error when you click "CommandButton1"
    You need to define Target. Since the caption for the button is "Relevant Data" you need to tell the macro what columns are the Relevant Data. Something like this
    Please Login or Register  to view this content.
    Based on your picture I can't tell what the Relevant Data is. If you have more that 1 Relevant Data (like maybe the Relevant Data changes with the Date, or Part No, or Time of Day, etc.) then 1 button might have a lot of trouble unhiding ONLY the Relevant Data.

  23. #23
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Conditionally Hide Columns based on cell result

    Is this not a solved thread?

  24. #24
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hide Columns based on cell result

    Quote Originally Posted by davesexcel View Post
    Is this not a solved thread?
    The original question was answered and he marked it solved, but then realized that he really wanted more that he thought. He tried to improve on the answer himself, but got confused, so he asked me to help with the addition.

  25. #25
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hide Columns based on cell result

    Quote Originally Posted by foxguy View Post
    The original question was answered and he marked it solved, but then realized that he really wanted more that he thought. He tried to improve on the answer himself, but got confused, so he asked me to help with the addition.
    Exactly, The original code posted by Dave worked, But I realized that I also wanted to be able to manually unhide cells, and then decided I wanted to make the original code also toggled to a button vs. based off cell updates. I can make a new thread If both of you think that is appropriate

  26. #26
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Conditionally Hide Columns based on cell result

    Quote Originally Posted by montego View Post
    I can make a new thread If both of you think that is appropriate
    I have no problem with leaving it the way it is. This has happened to me before, and this is the first time that a moderator has said anything about it.

    My thoughts are that since the 2nd question is directly related to the original solution, I would leave it here. Any one who looks at the thread because they saw the title, could very easily be interested in the follow up, but I'll follow the moderator's advice.

  27. #27
    Registered User
    Join Date
    06-30-2010
    Location
    Tucson, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Conditionally Hide Columns based on cell result

    I have created a new thread to deal with this topic. It can be found here: Hiding and Unhiding Entire Columns based on Cell Reference

  28. #28
    Registered User
    Join Date
    06-28-2011
    Location
    Vermont, USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Code WORKS

    Foxguy...

    This thread has been very useful as I have a similar need. I am trying to hide columns when the value in a row is greater than value in a specfic cell. I modified the code you provided a bit to achieve this but somehow just can get it to fire. As I change the value in the specified cell the columns remain visible. Here is the modified version of the code you provided.

    Please Login or Register  to view this content.
    Thanks very much for your help!

    --MrV

+ 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