+ Reply to Thread
Results 1 to 4 of 4

Hide columns based on specific cell value

Hybrid View

  1. #1
    Registered User
    Join Date
    05-29-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    58

    Hide columns based on specific cell value

    Hi I've searched the forum but can't find a specific way of doing this. I'm a beginner when it comes to VBA so would appreciate the help here.

    I have formula in cells C58:CJ58 - the result will either be zero or a number. I would like a code that, everytime that sheet is opened, will hide any column where the cell on row 58 is zero. Ideally I don't want this to affect other columns in the sheet, but if it does, I can sort that out.

    Is this possible?

    Thanks in advance!

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Hide columns based on specific cell value

    Try this - it needs to go in the worksheet object in the visual basic editor.

    Private Sub Worksheet_Activate()
    Application.ScreenUpdating = False
    For Each cl In Range("C58:CJ58").Cells
        If cl.Value = 0 Then
            Columns(cl.Column).Hidden = True
        Else
            Columns(cl.Column).Hidden = False
        End If
    Next
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    05-29-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: Hide columns based on specific cell value

    Thankyou both. I used the first one simply becaue it was first, but they both work.

    Thanks again,
    Sam

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Hide columns based on specific cell value

    hi samcdavies, different option
    Private Sub Worksheet_Activate()
    Dim rng2hide As Range, cl
    Range("c58:cj58").Columns.Hidden = 0
    For Each cl In Range("c58:cj58")
        If cl = 0 Then
            If rng2hide Is Nothing Then Set rng2hide = cl Else Set rng2hide = Union(rng2hide, cl)
        End If
    Next
    If Not rng2hide Is Nothing Then rng2hide.Columns.Hidden = 1
    End Sub
    Last edited by watersev; 05-14-2013 at 05:58 AM.

+ 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