Microsoft Knowledge Base

XL5: Recorded Macro Unexpectly Hides All Columns in Range

Last reviewed: September 13, 1996
Article ID: Q122061
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for the Macintosh, version 5.0

SYMPTOMS

In Microsoft Excel, when you record a Visual Basic, Applications Edition, macro that selects nonadjacent cells and hides them, you may not receive the results you expect when you run the recorded macro. When you run the macro, the entire range of columns between the nonadjacent selections will be hidden.

CAUSE

This problem occurs because the macro records the following statement:

   Selection.EntireColumn.Hidden

This statement hides every column between the upper-left cell and the lower- right cell of the selected cells (rather than hiding only the columns that contain the selected cells).

WORKAROUND

Microsoft provides examples of Visual Basic 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. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

To work around this problem, edit the macro such that it uses a For Each...Next statement to apply the Hidden property to only the columns that contain the selected cells, as in the following example:

Sub HideIt()
   For Each x in Selection.Areas
      x.EntireColumn.Hidden=True
   Next x
End Sub


KBCategory: kbprg kbcode
KBSubcategory:

Additional reference words: 5.00 5.00c 7.00 discontiguous discontinuous non-
adjacent non not contiguous non-contiguous



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: September 13, 1996
©1997 Microsoft Corporation. All rights reserved. Legal Notices.