+ Reply to Thread
Results 1 to 4 of 4

VBA to auto hide worksheets

Hybrid View

  1. #1
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    VBA to auto hide worksheets

    Hello All,
    I am attempting to configure a code to auto hide all worksheets in a workbook that do not have a value in cell A2, but leaves the sheets that do have a value in cell A2 visible. I want this to happen when the user cliucks the save button.
    Here is what I have so far:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim ws As Worksheet  ' Declares variable
     
    For Each ws In ThisWorkbook.Worksheets  'Start looping through all worksheets
     
    If Range("A2") = "" Then   Check if each worksheet range is empty
     ws.Visible = False
    
    End If
     
    Next ws  'Loop to next worksheet
    
    End Sub
    When I place this code in the workbook module and click save, nothing will happen to to any sheets when I have an active sheet that does have a value in A2, but when I have a visible sheet that does not have a value in A2, the code closes all worksheets in order up to the worksheet that is visible.
    Any help to point me in the right direction would be greatly appreciated!
    Thanks!
    Last edited by Pierce Quality; 11-06-2013 at 01:46 PM.

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

    Re: VBA to auto hide worksheets

    Try your code with ws.activate

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim ws As Worksheet  ' Declares variable
     
    For Each ws In ActiveWorkbook.Worksheets  'Start looping through all worksheets
    ws.Activate
    If Range("A2").Value = "" Then   'Check if each worksheet range is empty
     ws.Visible = False
    
    End If
     
    Next ws  'Loop to next worksheet
    End Sub

  3. #3
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA to auto hide worksheets

    Quote Originally Posted by JOHN H. DAVIS View Post
    Try your code with ws.activate

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim ws As Worksheet  ' Declares variable
     
    For Each ws In ActiveWorkbook.Worksheets  'Start looping through all worksheets
    ws.Activate
    If Range("A2").Value = "" Then   'Check if each worksheet range is empty
     ws.Visible = False
    
    End If
     
    Next ws  'Loop to next worksheet
    End Sub
    Thanks John, that worked great, appreciate it!

    Marking this as solved.

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

    Re: VBA to auto hide worksheets

    You're welcome. Glad to help out and thanks for the feedback.

+ 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. Auto hide & protect worksheets before closing
    By magnum206 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-11-2014, 08:32 PM
  2. Can I auto hide/unhide worksheets based on selected cell criteria.
    By cwashburn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2013, 04:06 PM
  3. Auto Hide Row
    By rajeev.raj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2012, 07:47 AM
  4. Auto hide worksheets from cell data input.
    By alzictorini in forum Excel General
    Replies: 9
    Last Post: 10-03-2011, 01:56 PM
  5. Auto Row Hide
    By holry7778 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2009, 09:21 AM

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.6.0 RC 1