Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Using ColorIndex Property to Set Color of Borders Results in Unexpected Behavior


View products that this article applies to.

This article was previously published under Q213688

↑ Back to the top


Symptoms

In Microsoft Excel 2000, if you run a Microsoft Visual Basic for Applications macro that uses the ColorIndex property to specify a color for the borders of a cell, some of the borders may not use that color or may otherwise appear differently than expected. For example, this behavior occurs when you use the following sample code in a macro:
ActiveCell.Borders.ColorIndex = 3
				

↑ Back to the top


Cause

This behavior can occur because Microsoft Excel 2000 applies the specified color to only the left, right, top, and bottom borders.

↑ Back to the top


Workaround

Microsoft provides programming examples 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 article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS To work around this issue, use the following macros 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 that 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.

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


More information

In Microsoft Excel 2000, 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: You can use the workaround in this article to force the diagonal borders to appear.

↑ Back to the top


Keywords: KB213688, kbprogramming, kbpending, kbdtacode, kbbug

↑ Back to the top

Article Info
Article ID : 213688
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 267