+ Reply to Thread
Results 1 to 3 of 3

Hiding a column based on 2 or more cell values

Hybrid View

  1. #1
    Registered User
    Join Date
    10-12-2010
    Location
    Leeds, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Hiding a column based on 2 or more cell values

    Hi,

    I am trying to hide column 'E' when either cell C8 or C16 is empty and show it when both cells are completed. The following code doesn't quite work.

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Address = "$C$16" And Target.Value = "" Or Target.Address = "$C$8" And Target.Value = "" Then
    Range("E:E").EntireColumn.Hidden = True
    
    ElseIf (Target.Address = "$C$16" And Target.Value <> "") And (Target.Address = "$C$8" And Target.Value <> "") Then
    Range("E:E").EntireColumn.Hidden = False
    
    End If
    End Sub
    I think its to do with the 'And' in the ElseIf statement but I don't know much about VBA so I could do with some help. Thanks
    Last edited by excelfan2010; 10-12-2010 at 09:16 AM.

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: Hiding a column based on 2 or more cell values

    Welcome to the forum!

    First, you need to use the "code" tags around your code. Edit your post and you can fix it.

    Second, here is a solution to your problem. Since the Target variable is for only a single range, your And is not working. This solution checks the C8 and C16 cells directly without using Target.
    Private Sub Worksheet_Change(ByVal Target As Range)
    If (Me.Cells(16, 3).Value <> "" And Me.Cells(8, 3).Value <> "") Then
        Range("E:E").EntireColumn.Hidden = True
    Else
        Range("E:E").EntireColumn.Hidden = False
    
    End If
    
    End Sub
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    10-12-2010
    Location
    Leeds, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Hiding a column based on 2 or more cell values

    Thanks Blane, it 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