+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Macro to hide sheet based on cell value within the sheet

  1. #1
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Macro to hide sheet based on cell value within the sheet

    Hello. I see that there are various posts related to my query, but I'm a novice when it comes to macros, and I'm not able to effectively use the code posted for someone else and modify it to my purposes, so I must apologize if my problem seems redundant.

    I'm trying to make a simple macro that will hide a sheet if the value in C1 is "True" based on a logic function that exams whether the date in C3 is equal to today's date. I would like this run when the sheet is opened if possible. I have the below code, but it doesn't seem to be working.

    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In Worksheets
    If Not ws.Range(C1) = "True" Then ws.Visible = False
    Next ws
    End Sub


    Any help would be greatly appreciated. Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    hi try use such code
    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In Worksheets
    If ws.Range("C1") <> "" Then
    If ws.Range("C1") = ws.Range("C3") Then
    ws.Visible = False
    End If
    End If
    Next ws
    End Sub
    p.s. the "True" in code is not equal to True as the result of logical function from cell so I put the logical equitation into code
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  3. #3
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    cancelled post
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  4. #4
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to hide sheet based on cell value within the sheet

    Quote Originally Posted by tom1977 View Post
    hi try use such code
    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In Worksheets
    If ws.Range("C1") <> "" Then
    If ws.Range("C1") = ws.Range("C3") Then
    ws.Visible = False
    End If
    End If
    Next ws
    End Sub
    p.s. the "True" in code is not equal to True as the result of logical function from cell so I put the logical equitation into code
    Hi Tom1977,

    Thanks for the quick reply. I put this code in the VBA editor, saved and closed the file. However, when I re-open the file, all of the tabs are showing, not just the ones where C1=True. Any thoughts on what I'm doing wrong? Thanks again!

  5. #5
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    look at attachment and tell me is it ok? if not put your description on my file. And remember that you can’t hide all sheets or workbook.
    Attached Files Attached Files
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  6. #6
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to hide sheet based on cell value within the sheet

    I opened the file, and I'm seeing 3 tabs. Essentially, what I'm trying to accomplish is to have only one tab visible at a time, and that tab would be "today's" tab. I'm attaching my file because I don't think I've done a very good job of explaining. Again, thanks so much!
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    Hi

    First You choosed wrong place to insert the code: You should put this into Thisworkbook module
    Second I changed code to this:

    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In Worksheets
    
    If ws.Range("C2") = Date Then
    ws.Visible = False
    End If
    
    Next ws
    End Sub
    with this You do not need any value in C1
    Attached Files Attached Files
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  8. #8
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to hide sheet based on cell value within the sheet

    I think this is close, but instead of only showing today's tab, it's only hiding today's tab. Thanks!

  9. #9
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    so change this
    If ws.Range("C2") = Date
    into this
    If ws.Range("C2") <> Date
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  10. #10
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Wink Re: Macro to hide sheet based on cell value within the sheet

    It worked! Thank you so much! I know that this was probably frustrating for you, but I really appreciate all of your help and patience!

  11. #11
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    Do not worry I am not frustrated good luck with VBA
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  12. #12
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to hide sheet based on cell value within the sheet

    Hi again,

    I'm sorry to bother you, but when I opened the file this morning to make sure that yesterday's sheet would hide, and today's would open, it got a run time error of 1004. When I go in to VBA, it appears that the problem is in the "ws.visible=false" line, as it is highlighted in yellow. Do you have any thoughts on what would be causing that error?

    Thanks so much!

  13. #13
    Valued Forum Contributor
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    895

    Re: Macro to hide sheet based on cell value within the sheet

    Are you sure at my computer is ok
    which line is indicated as error?
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  14. #14
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to hide sheet based on cell value within the sheet

    Quote Originally Posted by tom1977 View Post
    Are you sure at my computer is ok
    which line is indicated as error?

    It's working fine now. Sorry! But I do have another question. It came up today that people would like to see all the tabs for the coming week, so it would be a rolling 7 days worth of tabs showing. Is that possible to accomplish that by adjusting the code you've already given me? Again, thank you so much!

  15. #15
    Registered User
    Join Date
    12-08-2011
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Macro to hide sheet based on cell value within the sheet

    Hi again Tom1977,

    I'm getting the debut error when I try to open the file and have the macro run instantly. It's saying that the "ws.visible=False" line isn't working right. Any thoughts?

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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