+ Reply to Thread
Results 1 to 8 of 8

Hide Sheet Based on Cell Value

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    3

    Hide Sheet Based on Cell Value

    Hi I have tried searching the forum, however, I am struggling with this one.

    I want to hide a sheet based on a cell value if possible.

    I have created a weekly planner and in cell B3 on sheet1 I a would type in a property address. I want the code to hide Sheet2 if nothing is entered into the cell B3 and if I do enter any text in B3 then it will re appear.

    help would be appreciated

    Kind regards

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Hide Sheet Based on Cell Value

    Put this under sheet1

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = Range("B3").Address Then
        If Target.Value = "" Then
            Sheets("Sheet2").Visible = False
        Else
            Sheets("Sheet2").Visible = True
        End If
    End If
    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Hide Sheet Based on Cell Value

    Maybe:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    If Not Intersect(Target, Range("B3")) Is Nothing Then
        If Target.Value = "" Then
                Sheets("Sheet2").Visible = False
                ws.Activate
        Else
                    Sheets("Sheet2").Visible = True
                ws.Activate
        End If
    End If
            
    End Sub
    BTW. Welcome to the Forum.

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Hide Sheet Based on Cell Value

    Hi,

    Give it a try to this code. place it under the "Sheet1" module of the project.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Intersect(Target, Range("B3")) Is Nothing Then Exit Sub
    
    If Target.Value = vbNullString Then
        Sheets("Sheet2").Visible = False
    Else
        Sheets("Sheet2").Visible = True
    End If
    
    End Sub
    Thanks

    CopytoWorksheetModule.gif

  5. #5
    Registered User
    Join Date
    06-21-2012
    Location
    California
    MS-Off Ver
    Excel 2002 (XP)
    Posts
    3

    Re: Hide Sheet Based on Cell Value

    Quote Originally Posted by fredlo2010 View Post
    Hi,

    Give it a try to this code. place it under the "Sheet1" module of the project.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Intersect(Target, Range("B3")) Is Nothing Then Exit Sub
    
    If Target.Value = vbNullString Then
        Sheets("Sheet2").Visible = False
    Else
        Sheets("Sheet2").Visible = True
    End If
    
    End Sub
    Thanks

    Attachment 276349
    That's overly complicated ... there is no need to use the INTERSECT function.

    Also, if there IS data in B3 and it is deleted by selecting a range of cells that includes B3, then pressing "Delete" then macro fails.

    Try this instead...

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Range("B3").Value = vbNullString Then
        Sheets("Sheet2").Visible = False
    Else
        Sheets("Sheet2").Visible = True
    End If
    
    End Sub

  6. #6
    Registered User
    Join Date
    11-07-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Hide Sheet Based on Cell Value

    Hi thanks for the response I know it was a simple task for most but I could not get my head round it yesterday. I worked my way down the replies and J Davis worked how I wanted it to ...

    thanks again people

  7. #7
    Registered User
    Join Date
    11-07-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Hide Sheet Based on Cell Value

    I have noticed though that once my macro is running if I paste text into B3 then I get a type mismatch error. Same applies if I select the cell and press delete. To overcome this I have to select the text and press delete

    any ideas?

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Hide Sheet Based on Cell Value

    Try this modification. If it doesn't work then can you attach a sample with data which doesn't work?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    If Not Intersect(Target, Range("B3")) Is Nothing Then
        If Target.Text = "" Then
                Sheets("Sheet2").Visible = False
                ws.Activate
        Else
                    Sheets("Sheet2").Visible = True
                ws.Activate
        End If
    End If
            
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hide/Unhide Sheet based on cell value
    By mmogharreban in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-17-2022, 06:28 PM
  2. Automatically Hide Row based on Cell in another sheet
    By excel233 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-10-2015, 03:49 AM
  3. [SOLVED] Hide / Unhide Row on a Sheet based on the Value of a Cell on Another Sheet
    By neslotna in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2013, 12:23 PM
  4. Macro to hide sheet based on cell value within the sheet
    By a_dub_green in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 01-04-2012, 12:18 PM
  5. Hide sheet based on yes/no cell
    By Ace Rimmer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-08-2010, 06:53 AM

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