XL97: Run-Time Error 9 When Using a Macro to Remove Page Break

Last reviewed: January 9, 1998
Article ID: Q170635
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

In Microsoft Excel 97, if you run a Visual Basic for Applications macro that attempts to remove a vertical or horizontal page break from a worksheet, you may receive the following error message:

   Run-time error '9':
   Subscript out of range

CAUSE

This problem occurs if the following conditions are true:

  • If the page break is a vertical page break, the problem occurs if no data is located to the right of the page break.

        -or-
    
  • If the page break is a horizontal page break, the problem occurs if no data is located below the page break.

        -and-
    
  • The macro uses either VPageBreaks(x).Delete or HPageBreaks(x).Delete to delete a page break. For example, either of these lines of code may fail:

          ActiveSheet.VPageBreaks(1).Delete
    

          ActiveSheet.HPageBreaks(2).Delete
    

WORKAROUND

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.

If you know which column is immediately to the right of a vertical page break, or which row is immediately beneath a horizontal page break, you can remove the page break by setting the PageBreak property of the column or row to xlNone.

For example, to remove a vertical page break to the left of column H, use a line of code similar to the following:

   ActiveSheet.Columns("H").PageBreak = xlNone

To remove a horizontal page break below row 17, use the following code:

   ActiveSheet.Rows(17).PageBreak = xlNone

You can also prevent the problem from occurring by entering data either to the right of or below manual page breaks in worksheets.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

In Microsoft Excel 97, you can use the Delete method of the VPageBreaks and HPageBreaks collections to delete page breaks from worksheets. To use this method, it is not necessary to know the exact location of the page break in a worksheet; it is necessary to know only the page break you want to delete (for example, the first, the second, and so on). For example, to delete the sixth vertical page break from the active worksheet, you could use the following line of code:

   ActiveSheet.VPageBreaks(6).Delete

Note that this method does not work if no data exists to the right of the page break. Although deleting (or not deleting) such a page break has no effect when you print the worksheet, you may want to delete extra page breaks to keep the worksheets neat in appearance.


Additional query words: XL97
Keywords : kberrmsg
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbprb


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: January 9, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.