XL: RGB Function May Map to Incorrect Color

Last reviewed: January 9, 1998
Article ID: Q170781
The information in this article applies to:
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel for Windows, versions 5.0, 5.0c

SUMMARY

When you use the RGB function in a Visual Basic for Applications, 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.

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)).

MORE INFORMATION

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.

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. Click the Rectangle tool on the Drawing toolbar. Click cell C1 and drag the pointer from cell C1 to cell D5 to draw a rectangle.

  3. On the Tools menu, click Options. Then, click the Color tab.

  4. In the Standard colors group, click the first color in the second row (the red color).

  5. Click Modify. Then, click the Custom tab. In the Red box, change the value to 65. Click OK.

    Notice that the color is dark red.

  6. Click Modify. Then, click the Custom tab. Change the value of the Red box to 64. Click OK.

    Notice that the color is a little darker or duller than before.

  7. Click Reset. Then, close the Options dialog box by clicking OK.

  8. On Sheet1, enter the value 65 in cell B1.

  9. 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.

  10. Change the value in cell B1 to 64.

  11. 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.

NOTE: You can work around this problem in Microsoft Excel 97 for Windows. The ShapeRange objects collection allows greater flexibility when you change colors in code. Instead of using Selection.Interior.Color = RGB(x,y,z), you can use Selection.ShapeRange.Fill.ForeColor.RGB = RGB(x,y,z).

REFERENCES

For more information about the ShapeRange Object collection, click the Index tab in Microsoft Visual Basic Help, type the following text

  ShapeRange

and then double-click the selected text to go to the "ShapeRange Object Collection" topic.


Additional query words: XL97
Keywords : kbcode kbprg
Version : WINDOWS:5.0,5.0c,7.0,97
Platform : WINDOWS
Issue type : kbinfo


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.