Consulting

Results 1 to 8 of 8

Thread: Hiding worksheets based on one cell

  1. #1
    VBAX Newbie
    Joined
    Feb 2006
    Posts
    4
    Location

    Exclamation Hiding worksheets based on one cell

    I have an Excel worksheet with a Yes / No dropdown box - If the box is changed to "No" I need 2 of the other sheets in the book to be grayed out and/or inactivated.

    How would I accomplish this? I have used VB.Net alot but not much Excel VBA at all. Actually I am not even very sure how to reference cells from the VBA interface within Excel (the Help is not that helpful).

    Any expert advice and opinions would surely be welcome.

  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    221
    Location
    You could have something like this :

    [vba]
    If sheets.sheet("sheetname").range(a2) = "No" then
    sheetname.visible = false
    else sheetname.visible = true
    End if
    [/vba]

    I am not in excel as we speak, so the code may not be exact, but it would work, or point you in the righgt direction.

    P.S - that code, will actually hide the sheet completey. I am not sure about , deactivating it. You could try , " sheetname.enabled = false " , never had a use for it , so I am not 100% on that one.

    I am sure there is a better way to do this, but here is a basic way that would work also :

    [vba]
    If sheets.sheet("sheetname").range(a2) = "No" then
    sheetname.visible = false
    End if
    If sheets.sheet("sheetname").range(a2) = "YES" then
    sheetname.visible = True
    End if

    [/vba]

  3. #3
    VBAX Newbie
    Joined
    Feb 2006
    Posts
    4
    Location
    So what event would I enter that under for the particular sheet? I would like it to occur as soon as the drop down is changed from yes to no or vice versa.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Optional methods included.
    Regards
    MD

    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$3" Then
    If UCase(Target) = "NO" Then
    Sheets("Sheet2").Visible = False
    Sheets("Sheet3").Visible = False
    Sheets("Sheet4").Protect
    Sheets("Sheet5").Protect
    Else
    Sheets("Sheet2").Visible = True
    Sheets("Sheet3").Visible = True
    Sheets("Sheet4").Unprotect
    Sheets("Sheet5").Unprotect
    End If
    End If
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Newbie
    Joined
    Feb 2006
    Posts
    4
    Location
    Ok, I think I've almost got this....using mdmackillop's code above I am getting a subscript out of range error. I added several "sheets(sheet#).protect = true" statements to line it up with whats in my workbook, but did I miss something.

    Thanks for all the help it is very much appreciated.
    Last edited by JDizzle; 02-28-2006 at 12:58 PM.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    You would get that error if the sheet does not exist. If you click on the debug option, the offending item should be highlighted.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Newbie
    Joined
    Feb 2006
    Posts
    4
    Location
    Ah very good....it works perfectly thank you very much for the help!

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Glad to help.
    You can mark the thread Solved using Thread Tools at the head of the page.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •