+ Reply to Thread
Results 1 to 28 of 28

Conditionally Hide Columns based on cell result

Hybrid View

  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.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Range("B3").Value = 0 Then
            Columns("B").EntireColumn.Hidden = True
        Else
            Columns("B").EntireColumn.Hidden = False
        End If
    End Sub
    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
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Count > 1 Then Exit Sub
        If Union(Range("$B$1:$BA$100"), Target).Address = Range("$B$1:$BA$100").Address Then
            If Target.Value = 0 Then Target.EntireColumn.Hidden = True
        End If
    End Sub

  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
        If Union(Range("$B$1:$BA$100"), Target).Address = Range("$B$1:$BA$100").Address Then
    Be
       If Union(Range("$B$3:$BA$3"), Target).Address =  Range("$B$3:$BA$3").Address Then


    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...

    Sub HideEmptyColumns()
    
    Dim c As Integer
    For c = 2 To 52
    If IsEmpty(Cells(3, c)) Then
    Columns(c).EntireColumn.Hidden = True
    End If
    Next c
    
    End Sub
    Sub HideColumnB1()
        If Range("B3").Value = 0 Then
            Columns("B").EntireColumn.Hidden = True
        Else
            Columns("B").EntireColumn.Hidden = False
        End If
    End Sub

  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
     Private Sub Worksheet_Change(ByVal Target As Range)
    I changed the code to...

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Count > 1 Then Exit Sub
        If Union(Range("$B$1:$BA$100"), Target).Address = Range("$B$1:$BA$100").Address Then
            If Target.Value = 0 Then Target.EntireColumn.Hidden = True
        End If
    End Sub
    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 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?

  11. #11
    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.

  12. #12
    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

  13. #13
    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.

  14. #14
    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

+ 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