conditional hiding of rows and/or columns

jtolksdo
jtolksdo used Ask the Experts™
on
Hi,

I'd like to hide rows and/or columns based on some cell contents, after these contents are entered. I fill the cell contents with check-boxes by adding a cell-link from the check-box to a cell.
If the box is checked, the cell shows the value true.


If I want to do this based on one expression I use the VBA Code as follos:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
     ' Dim R As Range
     ' Set R = Application.Intersect (Target, Range("B7:B7"))
     ' If R Is Nothing Then Exit Sub
    With ActiveSheet
        For Each Cell In Range("C7")
            If Cell.Value = "explicit" Then
                Cell.EntireColumn("F").Hidden = True
                Cell.EntireColumn("E").Hidden = True
                Cell.EntireColumn("G").Hidden = False
            Else
                Cell.EntireColumn("G").Hidden = True
                Cell.EntireColumn("E").Hidden = False
                Cell.EntireColumn("F").Hidden = False
            End If
            Next
    End With
    Application.ScreenUpdating = True
End Sub
---

Now I'd like to add something like this:
    With ActiveSheet
        For Each Cell In Range("C11")
            If Cell.Value = "true" Then
                Cell.EntireColumn("I").Hidden = True
            Else
                Cell.EntireColumn("I").Hidden = False
            End If
----
Unfortunately I don't know how (my last code might be totally wrong).
Please advise.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hi jtolksdo,

Firstly, it seems that with this line:
        For Each Cell In Range("C7")
that you are only "looping" through one cell, which is uneccessary.  Here is a slightly smaller version:

   With ActiveSheet
           ' This will check Row 7, Column 3, which is C7
            If LCase(Cells(7, 3).Value) = "explicit" Then
                Cell.EntireColumn("F").Hidden = True
                Cell.EntireColumn("E").Hidden = True
                Cell.EntireColumn("G").Hidden = False
            Else
                Cell.EntireColumn("G").Hidden = True
                Cell.EntireColumn("E").Hidden = False
                Cell.EntireColumn("F").Hidden = False
            End If
    End With

Now to hide a column, based on the value of C11, use
If Cells(11, 3).Value = "true" Then
  Columns(3).Hidden = True
Else
  Columns(3).Hidden = False
End If

Regards,

Rob.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial