Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/107623

From BetaArchive Wiki

Microsoft Knowledge Base

XL: Error Unhiding Multiple Sheets in Visual Basic

Last reviewed: June 13, 1997
Article ID: Q107623

The information in this article applies to:

  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, version 7.0

SUMMARY

In the Microsoft Visual Basic Programming System, Applications Edition, it is not possible to unhide multiple sheets with a single command. If you try to change the Visible property for more than one sheet to True in a single command such as following

   Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Visible = True

you will receive the error:

   Unable to set the Visible property of the Sheets class

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400. The following code examples assume you have a workbook that contains three worksheets (Sheet1, Sheet2, and Sheet3) and a single Visual Basic module (Module1).

If you run the HideSheets subroutine, the sheet tabs for Sheet1, Sheet2, and Sheet3 will disappear from the screen as the sheets are hidden.

To unhide the sheets, you can use the UnhideSheets subroutine. This subroutine loops through an array to unhide the sheets.

'---------------------------------------------------------------------

Option Explicit

Sub HideSheets()
   'This line hides the sheets listed in the array
   'by setting the Visible property to False.
    Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Visible = False
End Sub

Sub UnhideSheets()
   'Dimension some variables.
   Dim Collection As Variant, Item As Variant
   'This line creates an array of sheets, called "Collection".
   Collection = Array("Sheet1", "Sheet2", "Sheet3")
   'Iterate through the loop once for each sheet in the array.
   For Each Item In Collection           'for each sheet in the array,
      Sheets(Item).Visible = True        'unhide the current sheet
   Next                                  'repeat the loop until all done
End Sub
'---------------------------------------------------------------------

By establishing an array (called "Collection" in this example) and using a For Each loop, it is possible to unhide the sheets one at a time. If you are unhiding many sheets, you may want to use the following line of code to disable your screen while the sheets are being unhidden:

   Application.ScreenUpdating = False

To re-enable the screen when you are done, use the following command:

   Application.ScreenUpdating = True

Additional reference words: 5.00 7.00

Keywords : kbprg PgmOthr
Version : 5.00 5.00c 7.00 | 5.00 5.00a
Platform : MACINTOSH WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 13, 1997
©1997 Microsoft Corporation. All rights reserved. Legal Notices.