XL97: Problems Using ColorIndex to Set Color of Borders

Last reviewed: March 13, 1998
Article ID: Q171953
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 uses ColorIndex property to specify a color for the borders of a cell, some of the borders may fail to use that color or may not appear. For example, this problem occurs when you use the following sample code in a macro:

   ActiveCell.Borders.ColorIndex = 3


CAUSE

This problem occurs because Microsoft Excel 97 applies the specified color to only the left, right, top, and bottom borders.

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.

The following example code uses the ColorIndex property to specify a color for the borders of a cell or a range of cells.

Left, Right, Top, and Bottom Borders

To use the ColorIndex property for these types of borders, set the ColorIndex property to the value you want, for example:

   ActiveCell.Borders(xlEdgeLeft).ColorIndex = 2
   ActiveCell.Borders(xlEdgeRight).ColorIndex = 3
   ActiveCell.Borders(xlEdgeTop).ColorIndex = 4
   ActiveCell.Borders(xlEdgeBottom).ColorIndex = 5

You can set all four borders to the same value by using a line of code similar to the following:

   ActiveCell.Borders.ColorIndex = 6

Inside Vertical and Inside Horizontal Borders

To use the ColorIndex property for these types of borders, set the ColorIndex property for the appropriate constant to the correct value, for example:

   Selection.Borders(xlInsideVertical).ColorIndex = 7
   Selection.Borders(xlInsideHorizontal).ColorIndex = 8

NOTE: When you use the xlInsideVertical constant, the selected range must contain at least two columns. When you use the xlInsideHorizontal constant, the selected range must contain at least two rows. If the selection does not contain the necessary number of columns or rows, you receive the following error message:

   Run-time error '1004':
   Unable to set the ColorIndex property of the Border class

Diagonal Up and Diagonal Down Borders

To use the ColorIndex property for these types of borders, first set the Weight property of the border, for example:

   With Selection.Borders(xlDiagonalUp)
       .Weight = xlThin
       .ColorIndex = 9
   End With

-or-

   Selection.Borders(xlDiagonalDown).Weight = xlMedium
   Selection.Borders(xlDiagonalDown).ColorIndex = 10

NOTE: If you use the ColorIndex property of the diagonal border before you set its Weight property, the ColorIndex property is ignored, and the border appears in the default color (black). If you do not set the Weight property, the border does not appear.

STATUS

This behavior, with the exception of the behavior of diagonal borders, is by design of Microsoft Excel 97.

Microsoft has confirmed the behavior of diagonal borders 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, if you run a macro with the following line of code

   MsgBox ActiveCell.Borders.Count

a message box with the number of borders for the active cell appears. By running this macro, you can determine that a cell contains six borders: left, right, top, bottom, diagonal up, and diagonal down. However, if you specify a color for all six borders by running a macro that uses the following line of code

   ActiveCell.Borders.ColorIndex = 3

the color for only the first four borders is changed. The color for the diagonal borders is not changed because no weight is specified for them. Note that you can use the workaround in this article to force the diagonal borders to appear.


Additional query words: XL97
Keywords : kbcode kbprg
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbbug
Solution Type : kbworkaround


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