+ Reply to Thread
Results 1 to 7 of 7

Thread: Hide Unhide Columns on certain cell value

  1. #1
    Registered User
    Join Date
    11-22-2011
    Location
    Noida
    MS-Off Ver
    Excel 2007
    Posts
    21

    Question Hide Unhide Columns on certain cell value

    Hi,

    I need to write a macro to hide and unhide columns in multiple speadsheets on particular cell value (say Yes/No).

    I have 6 excel worksheets - Sheet 1 - Sheet 6.
    In first worksheet (Sheet 1), I have given a condition to be provided by user in terms of "Yes" or "No".

    In case, if in the first worksheet the condition provided is "Yes", I have to hide Coumns F, G, H and K in other five worksheets.
    Last edited by gmalpani; 11-24-2011 at 10:17 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Hide Unhide Columns on certain cell value

    Hi,

    Try this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim wsEachSheet As Worksheet
        If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
        For Each wsEachSheet In ThisWorkbook.Worksheets
            If wsEachSheet.Name <> "Sheet1" Then
                If UCase(Range("A1").Value) = "YES" Then
                    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True
                ElseIf UCase(Range("A1").Value) = "NO" Then
                    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False
                End If
            End If
        Next wsEachSheet
    End Sub

    Right click on the Sheet1's tab and select View Code. Change A1 to the cell that will have Yes/No in it.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    11-22-2011
    Location
    Noida
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Hide Unhide Columns on certain cell value

    Hi Dom,

    Thanks for the quick reply .. It really helps !

    In this macro, it will hide the columns in all the worksheets.
    In case, I have to hide columns in only WS 2, 3, 4 and 5 and rest ws 6 and 7 should not be affected. How can I make this change in the macro provided by you.

    Br,
    Gaurav

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Hide Unhide Columns on certain cell value

    Try this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim wsEachSheet As Worksheet
        If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
        For Each wsEachSheet In Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5"))
                If UCase(Range("A1").Value) = "YES" Then
                    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True
                ElseIf UCase(Range("A1").Value) = "NO" Then
                    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False
                End If
        Next wsEachSheet
    End Sub

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    11-22-2011
    Location
    Noida
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Hide Unhide Columns on certain cell value

    Hi Dom,

    I need to modify the macro little bit.
    In addition to the columns, I have to hide Row 89 - Row 99.

    Below statements is for hiding columns in respective worksheet. Can you modify it to hide rows 89 - 99 as well. I have tried to modify this, but I think there is some error and it requires some AND operation in between.

    If UCase(Range("A1").Value) = "YES" Then
    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True
    wsEachSheet.Range("89:99").EntireRow.Hidden = True
    ElseIf UCase(Range("A1").Value) = "NO" Then
    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False
    wsEachSheet.Range("89:99").EntireRow.Hidden = True

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Hide Unhide Columns on certain cell value

    Close, you just needed to change True to False to unhide the columns:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim wsEachSheet As Worksheet
        If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
        For Each wsEachSheet In Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5"))
                If UCase(Range("A1").Value) = "YES" Then
                    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True
                    wsEachSheet.Range("89:99").EntireRow.Hidden = True
                ElseIf UCase(Range("A1").Value) = "NO" Then
                    wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False
                    wsEachSheet.Range("89:99").EntireRow.Hidden = False
                End If
        Next wsEachSheet
    End Sub

    Please remember to use code tags when posting code to the forum.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  7. #7
    Registered User
    Join Date
    11-22-2011
    Location
    Noida
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Hide Unhide Columns on certain cell value

    Hi Dom,

    Thanks a lot for your time and support !

    I am a beginner in Excel Macros. Could you please suggest me from where I should start ?
    Does this forum provides some tutorials or docs which can help me.
    Last edited by gmalpani; 11-24-2011 at 10:15 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.2.0