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: RGB Function May Map to Unexpected Color


View products that this article applies to.

This article was previously published under Q213201

↑ Back to the top


Symptoms

When you use the RGBfunction in a Visual Basic for Applications macro, the color value of the RGB color of the index may be mapped to a color other than the color you expected.

For example, RGB(65,0,0) is mapped to Dark Red, but RGB(64,0,0) is mapped to Black.

↑ Back to the top


Cause

This behavior occurs because the color property accepts an RGB triple and maps it to the nearest color index. When the property retrieves the color value, it returns the RGB color of the index, which may be different from the value you typed. In the example, RGB(65,0,0) is mapped to Dark Red (RGB(128,0,0)), but RGB(64,0,0) is mapped to Black (RGB(0,0,0)).

↑ Back to the top


Workaround

To work around this problem, use the ShapeRange objects collection. The ShapeRange objects collection allows greater flexibility when you change colors in code. Instead of using this
Selection.Interior.Color = RGB(x,y,z)
				
you can use the following:
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(x,y,z)
				

↑ Back to the top


More information

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: When you use an application method or property that accepts a color specification value, use a number that represents an RGB color value. An RGB color value specifies the relative intensity of the red, green, and blue colors that cause a specific color to be displayed.

The following steps assume that you created a Visual Basic module that contains the following code
Sub ChangeRGB()

     ActiveSheet.Rectangles(1).Select
     With Selection.Interior
         .Pattern = xlSolid
         .Color = RGB([B1].Value, [B2].Value, [B3].Value)
     End With

End Sub
				
and that you created a worksheet that contains the following values:
   A1: Red     B1:       C1:       D1:
   A2: Green   B2:       C2:       D2:
   A3: Blue    B3:       C3:       D3:
   A4:         B4:       C4:       D4:
   A5:         B5:       C5:       D5:
				
To use the sample macro, follow these steps:
  1. On the View menu, point to Toolbars, and then click Drawing.
  2. On the Drawing toolbar, click the Rectangle tool. Click cell C1 and drag the pointer from cell C1 to cell D5 to draw a rectangle.
  3. On Sheet1, type the value 65 in cell B1.
  4. On the Tools menu, point to Macro, and then click Macros. Click ChangeRGB, and then click Run.

    Note that the color of the rectangle is dark red.
  5. Change the value in cell B1 to 64.
  6. On the Tools menu, point to Macro, and then click Macros. Click ChangeRGB, and then click Run.

    Note that the color of the rectangle is black.

↑ Back to the top


References

For more information about the ShapeRange collection object, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type ShapeRange Collection Object in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213201, kbprogramming, kbprb, kbdtacode

↑ Back to the top

Article Info
Article ID : 213201
Revision : 7
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 281