If cells in column are blank/not blank, hide/show other columns

  • Hi,

    I'm trying to write VBA code so that when the cells in column D are empty, the columns E to Z are hidden. When something is written in the cell the columns should unhide.

    Hiding the columns has already works, but unhiding doesn't seem to work.

    Anybody that can help with my problem?

    Code
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Columns(5)) Is Nothing Then
            If Target.Text = Blank Then
                Columns("I").EntireColumn.Hidden = True
            ElseIf Target.Text = NotBlank Then
                Columns("I").EntireColumn.Hidden = False
            End If
        End If
    End Sub
  • Your code is testing Column E.


    I don't know how how you expect it to work. Once a cell in D has a value entered it would open the range, but what about entries into other cells in D?


    The Change event will only run when an entry is made in D.


    You need something like this


    Code
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Columns(4)) Is Nothing Then Exit Sub
        If Target.Value = Empty Then
            Columns("I").EntireColumn.Hidden = True
            Else: Columns("E:Z").EntireColumn.Hidden = False
        End If
    End Sub
  • I indeed had a error in the initial code.
    After some trail and error i got my inital setup working using the following.

    Code
    If Not Intersect(Target, Columns(4)) Is Nothing Then
            If Target.Text = Blank Then
                Columns("F").EntireColumn.Hidden = True
            Else
                Columns("F").EntireColumn.Hidden = False
            End If
        End If

    However the method you suggested using "Target.value" also works. Thanks for the help!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!