• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

Identifying a color by name in VBA

Instead of:
cel.Interior.Color  = RGB(102, 0, 204)

I want to say:
cel.Interior.Color = Purple

How do I write that?

Thanks,
John
0
gabrielPennyback
Asked:
gabrielPennyback
  • 2
  • 2
2 Solutions
 
Martin LissRetired ProgrammerCommented:
You can't. There is a limited set of VBA named colors and they are vbBlack, vbBlue, vbCyan, vbGreen, vbMagenta,  vbRed,  vbWhite and vbYellow.
0
 
andrewssd3Commented:
This may help - although a little clumsy, you can adapt it to your needs - I have taken the CSS named colours and set up variables for the RGB versions, which you can then use to assign to variables.
Sub colors()
Dim aliceblue As Long '= RGB(240, 248, 255)
Dim antiquewhite As Long '= RGB(250, 235, 215)
Dim aqua As Long '= RGB(0, 255, 255)
Dim aquamarine As Long '= RGB(127, 255, 212)
Dim azure As Long '= RGB(240, 255, 255)
Dim beige As Long '= RGB(245, 245, 220)
Dim bisque As Long '= RGB(255, 228, 196)
Dim black As Long '= RGB(0, 0, 0)
Dim blanchedalmond As Long '= RGB(255, 235, 205)
Dim blue As Long '= RGB(0, 0, 255)
Dim blueviolet As Long '= RGB(138, 43, 226)
Dim brown As Long '= RGB(165, 42, 42)
Dim burlywood As Long '= RGB(222, 184, 135)
Dim cadetblue As Long '= RGB(95, 158, 160)
Dim chartreuse As Long '= RGB(127, 255, 0)
Dim chocolate As Long '= RGB(210, 105, 30)
Dim coral As Long '= RGB(255, 127, 80)
Dim cornflowerblue As Long '= RGB(100, 149, 237)
Dim cornsilk As Long '= RGB(255, 248, 220)
Dim crimson As Long '= RGB(220, 20, 60)
Dim cyan As Long '= RGB(0, 255, 255)
Dim darkblue As Long '= RGB(0, 0, 139)
Dim darkcyan As Long '= RGB(0, 139, 139)
Dim darkgoldenrod As Long '= RGB(184, 134, 11)
Dim darkgray As Long '= RGB(169, 169, 169)
Dim darkgreen As Long '= RGB(0, 100, 0)
Dim darkgrey As Long '= RGB(169, 169, 169)
Dim darkkhaki As Long '= RGB(189, 183, 107)
Dim darkmagenta As Long '= RGB(139, 0, 139)
Dim darkolivegreen As Long '= RGB(85, 107, 47)
Dim darkorange As Long '= RGB(255, 140, 0)
Dim darkorchid As Long '= RGB(153, 50, 204)
Dim darkred As Long '= RGB(139, 0, 0)
Dim darksalmon As Long '= RGB(233, 150, 122)
Dim darkseagreen As Long '= RGB(143, 188, 143)
Dim darkslateblue As Long '= RGB(72, 61, 139)
Dim darkslategray As Long '= RGB(47, 79, 79)
Dim darkslategrey As Long '= RGB(47, 79, 79)
Dim darkturquoise As Long '= RGB(0, 206, 209)
Dim darkviolet As Long '= RGB(148, 0, 211)
Dim deeppink As Long '= RGB(255, 20, 147)
Dim deepskyblue As Long '= RGB(0, 191, 255)
Dim dimgray As Long '= RGB(105, 105, 105)
Dim dimgrey As Long '= RGB(105, 105, 105)
Dim dodgerblue As Long '= RGB(30, 144, 255)
Dim firebrick As Long '= RGB(178, 34, 34)
Dim floralwhite As Long '= RGB(255, 250, 240)
Dim forestgreen As Long '= RGB(34, 139, 34)
Dim fuchsia As Long '= RGB(255, 0, 255)
Dim gainsboro As Long '= RGB(220, 220, 220)
Dim ghostwhite As Long '= RGB(248, 248, 255)
Dim gold As Long '= RGB(255, 215, 0)
Dim goldenrod As Long '= RGB(218, 165, 32)
Dim gray As Long '= RGB(128, 128, 128)
Dim green As Long '= RGB(0, 128, 0)
Dim greenyellow As Long '= RGB(173, 255, 47)
Dim grey As Long '= RGB(128, 128, 128)
Dim honeydew As Long '= RGB(240, 255, 240)
Dim hotpink As Long '= RGB(255, 105, 180)
Dim indianred As Long '= RGB(205, 92, 92)
Dim indigo As Long '= RGB(75, 0, 130)
Dim ivory As Long '= RGB(255, 255, 240)
Dim khaki As Long '= RGB(240, 230, 140)
Dim lavender As Long '= RGB(230, 230, 250)
Dim lavenderblush As Long '= RGB(255, 240, 245)
Dim lawngreen As Long '= RGB(124, 252, 0)
Dim lemonchiffon As Long '= RGB(255, 250, 205)
Dim lightblue As Long '= RGB(173, 216, 230)
Dim lightcoral As Long '= RGB(240, 128, 128)
Dim lightcyan As Long '= RGB(224, 255, 255)
Dim lightgoldenrodyellow As Long '= RGB(250, 250, 210)
Dim lightgray As Long '= RGB(211, 211, 211)
Dim lightgreen As Long '= RGB(144, 238, 144)
Dim lightgrey As Long '= RGB(211, 211, 211)
Dim lightpink As Long '= RGB(255, 182, 193)
Dim lightsalmon As Long '= RGB(255, 160, 122)
Dim lightseagreen As Long '= RGB(32, 178, 170)
Dim lightskyblue As Long '= RGB(135, 206, 250)
Dim lightslategray As Long '= RGB(119, 136, 153)
Dim lightslategrey As Long '= RGB(119, 136, 153)
Dim lightsteelblue As Long '= RGB(176, 196, 222)
Dim lightyellow As Long '= RGB(255, 255, 224)
Dim lime As Long '= RGB(0, 255, 0)
Dim limegreen As Long '= RGB(50, 205, 50)
Dim linen As Long '= RGB(250, 240, 230)
Dim magenta As Long '= RGB(255, 0, 255)
Dim maroon As Long '= RGB(128, 0, 0)
Dim mediumaquamarine As Long '= RGB(102, 205, 170)
Dim mediumblue As Long '= RGB(0, 0, 205)
Dim mediumorchid As Long '= RGB(186, 85, 211)
Dim mediumpurple As Long '= RGB(147, 112, 219)
Dim mediumseagreen As Long '= RGB(60, 179, 113)
Dim mediumslateblue As Long '= RGB(123, 104, 238)
Dim mediumspringgreen As Long '= RGB(0, 250, 154)
Dim mediumturquoise As Long '= RGB(72, 209, 204)
Dim mediumvioletred As Long '= RGB(199, 21, 133)
Dim midnightblue As Long '= RGB(25, 25, 112)
Dim mintcream As Long '= RGB(245, 255, 250)
Dim mistyrose As Long '= RGB(255, 228, 225)
Dim moccasin As Long '= RGB(255, 228, 181)
Dim navajowhite As Long '= RGB(255, 222, 173)
Dim navy As Long '= RGB(0, 0, 128)
Dim oldlace As Long '= RGB(253, 245, 230)
Dim olive As Long '= RGB(128, 128, 0)
Dim olivedrab As Long '= RGB(107, 142, 35)
Dim orange As Long '= RGB(255, 165, 0)
Dim orangered As Long '= RGB(255, 69, 0)
Dim orchid As Long '= RGB(218, 112, 214)
Dim palegoldenrod As Long '= RGB(238, 232, 170)
Dim palegreen As Long '= RGB(152, 251, 152)
Dim paleturquoise As Long '= RGB(175, 238, 238)
Dim palevioletred As Long '= RGB(219, 112, 147)
Dim papayawhip As Long '= RGB(255, 239, 213)
Dim peachpuff As Long '= RGB(255, 218, 185)
Dim peru As Long '= RGB(205, 133, 63)
Dim pink As Long '= RGB(255, 192, 203)
Dim plum As Long '= RGB(221, 160, 221)
Dim powderblue As Long '= RGB(176, 224, 230)
Dim purple As Long '= RGB(128, 0, 128)
Dim red As Long '= RGB(255, 0, 0)
Dim rosybrown As Long '= RGB(188, 143, 143)
Dim royalblue As Long '= RGB(65, 105, 225)
Dim saddlebrown As Long '= RGB(139, 69, 19)
Dim salmon As Long '= RGB(250, 128, 114)
Dim sandybrown As Long '= RGB(244, 164, 96)
Dim seagreen As Long '= RGB(46, 139, 87)
Dim seashell As Long '= RGB(255, 245, 238)
Dim sienna As Long '= RGB(160, 82, 45)
Dim silver As Long '= RGB(192, 192, 192)
Dim skyblue As Long '= RGB(135, 206, 235)
Dim slateblue As Long '= RGB(106, 90, 205)
Dim slategray As Long '= RGB(112, 128, 144)
Dim slategrey As Long '= RGB(112, 128, 144)
Dim snow As Long '= RGB(255, 250, 250)
Dim springgreen As Long '= RGB(0, 255, 127)
Dim steelblue As Long '= RGB(70, 130, 180)
Dim tan As Long '= RGB(210, 180, 140)
Dim teal As Long '= RGB(0, 128, 128)
Dim thistle As Long '= RGB(216, 191, 216)
Dim tomato As Long '= RGB(255, 99, 71)
Dim turquoise As Long '= RGB(64, 224, 208)
Dim violet As Long '= RGB(238, 130, 238)
Dim wheat As Long '= RGB(245, 222, 179)
Dim white As Long '= RGB(255, 255, 255)
Dim whitesmoke As Long '= RGB(245, 245, 245)
Dim yellow As Long '= RGB(255, 255, 0)
Dim yellowgreen As Long '= RGB(154, 205, 50)
 aliceblue = RGB(240, 248, 255)
 antiquewhite = RGB(250, 235, 215)
 aqua = RGB(0, 255, 255)
 aquamarine = RGB(127, 255, 212)
 azure = RGB(240, 255, 255)
 beige = RGB(245, 245, 220)
 bisque = RGB(255, 228, 196)
 black = RGB(0, 0, 0)
 blanchedalmond = RGB(255, 235, 205)
 blue = RGB(0, 0, 255)
 blueviolet = RGB(138, 43, 226)
 brown = RGB(165, 42, 42)
 burlywood = RGB(222, 184, 135)
 cadetblue = RGB(95, 158, 160)
 chartreuse = RGB(127, 255, 0)
 chocolate = RGB(210, 105, 30)
 coral = RGB(255, 127, 80)
 cornflowerblue = RGB(100, 149, 237)
 cornsilk = RGB(255, 248, 220)
 crimson = RGB(220, 20, 60)
 cyan = RGB(0, 255, 255)
 darkblue = RGB(0, 0, 139)
 darkcyan = RGB(0, 139, 139)
 darkgoldenrod = RGB(184, 134, 11)
 darkgray = RGB(169, 169, 169)
 darkgreen = RGB(0, 100, 0)
 darkgrey = RGB(169, 169, 169)
 darkkhaki = RGB(189, 183, 107)
 darkmagenta = RGB(139, 0, 139)
 darkolivegreen = RGB(85, 107, 47)
 darkorange = RGB(255, 140, 0)
 darkorchid = RGB(153, 50, 204)
 darkred = RGB(139, 0, 0)
 darksalmon = RGB(233, 150, 122)
 darkseagreen = RGB(143, 188, 143)
 darkslateblue = RGB(72, 61, 139)
 darkslategray = RGB(47, 79, 79)
 darkslategrey = RGB(47, 79, 79)
 darkturquoise = RGB(0, 206, 209)
 darkviolet = RGB(148, 0, 211)
 deeppink = RGB(255, 20, 147)
 deepskyblue = RGB(0, 191, 255)
 gray = RGB(105, 105, 105)
 grey = RGB(105, 105, 105)
 dodgerblue = RGB(30, 144, 255)
 firebrick = RGB(178, 34, 34)
 floralwhite = RGB(255, 250, 240)
 forestgreen = RGB(34, 139, 34)
 fuchsia = RGB(255, 0, 255)
 gainsboro = RGB(220, 220, 220)
 ghostwhite = RGB(248, 248, 255)
 gold = RGB(255, 215, 0)
 goldenrod = RGB(218, 165, 32)
 gray = RGB(128, 128, 128)
 green = RGB(0, 128, 0)
 greenyellow = RGB(173, 255, 47)
 grey = RGB(128, 128, 128)
 honeydew = RGB(240, 255, 240)
 hotpink = RGB(255, 105, 180)
 indianred = RGB(205, 92, 92)
 indigo = RGB(75, 0, 130)
 ivory = RGB(255, 255, 240)
 khaki = RGB(240, 230, 140)
 lavender = RGB(230, 230, 250)
 lavenderblush = RGB(255, 240, 245)
 lawngreen = RGB(124, 252, 0)
 lemonchiffon = RGB(255, 250, 205)
 lightblue = RGB(173, 216, 230)
 lightcoral = RGB(240, 128, 128)
 lightcyan = RGB(224, 255, 255)
 lightgoldenrodyellow = RGB(250, 250, 210)
 lightgray = RGB(211, 211, 211)
 lightgreen = RGB(144, 238, 144)
 lightgrey = RGB(211, 211, 211)
 lightpink = RGB(255, 182, 193)
 lightsalmon = RGB(255, 160, 122)
 lightseagreen = RGB(32, 178, 170)
 lightskyblue = RGB(135, 206, 250)
 lightslategray = RGB(119, 136, 153)
 lightslategrey = RGB(119, 136, 153)
 lightsteelblue = RGB(176, 196, 222)
 lightyellow = RGB(255, 255, 224)
 lime = RGB(0, 255, 0)
 limegreen = RGB(50, 205, 50)
 linen = RGB(250, 240, 230)
 magenta = RGB(255, 0, 255)
 maroon = RGB(128, 0, 0)
 mediumaquamarine = RGB(102, 205, 170)
 mediumblue = RGB(0, 0, 205)
 mediumorchid = RGB(186, 85, 211)
 mediumpurple = RGB(147, 112, 219)
 mediumseagreen = RGB(60, 179, 113)
 mediumslateblue = RGB(123, 104, 238)
 mediumspringgreen = RGB(0, 250, 154)
 mediumturquoise = RGB(72, 209, 204)
 mediumvioletred = RGB(199, 21, 133)
 midnightblue = RGB(25, 25, 112)
 mintcream = RGB(245, 255, 250)
 mistyrose = RGB(255, 228, 225)
 moccasin = RGB(255, 228, 181)
 navajowhite = RGB(255, 222, 173)
 navy = RGB(0, 0, 128)
 oldlace = RGB(253, 245, 230)
 olive = RGB(128, 128, 0)
 olivedrab = RGB(107, 142, 35)
 orange = RGB(255, 165, 0)
 orangered = RGB(255, 69, 0)
 orchid = RGB(218, 112, 214)
 palegoldenrod = RGB(238, 232, 170)
 palegreen = RGB(152, 251, 152)
 paleturquoise = RGB(175, 238, 238)
 palevioletred = RGB(219, 112, 147)
 papayawhip = RGB(255, 239, 213)
 peachpuff = RGB(255, 218, 185)
 peru = RGB(205, 133, 63)
 pink = RGB(255, 192, 203)
 plum = RGB(221, 160, 221)
 powderblue = RGB(176, 224, 230)
 purple = RGB(128, 0, 128)
 red = RGB(255, 0, 0)
 rosybrown = RGB(188, 143, 143)
 royalblue = RGB(65, 105, 225)
 saddlebrown = RGB(139, 69, 19)
 salmon = RGB(250, 128, 114)
 sandybrown = RGB(244, 164, 96)
 seagreen = RGB(46, 139, 87)
 seashell = RGB(255, 245, 238)
 sienna = RGB(160, 82, 45)
 silver = RGB(192, 192, 192)
 skyblue = RGB(135, 206, 235)
 slateblue = RGB(106, 90, 205)
 slategray = RGB(112, 128, 144)
 slategrey = RGB(112, 128, 144)
 snow = RGB(255, 250, 250)
 springgreen = RGB(0, 255, 127)
 steelblue = RGB(70, 130, 180)
 tan = RGB(210, 180, 140)
 teal = RGB(0, 128, 128)
 thistle = RGB(216, 191, 216)
 tomato = RGB(255, 99, 71)
 turquoise = RGB(64, 224, 208)
 violet = RGB(238, 130, 238)
 wheat = RGB(245, 222, 179)
 white = RGB(255, 255, 255)
 whitesmoke = RGB(245, 245, 245)
 yellow = RGB(255, 255, 0)
 yellowgreen = RGB(154, 205, 50)
 
 
    Selection.Interior.Color = purple
    

End Sub

Open in new window

0
 
Martin LissRetired ProgrammerCommented:
You probably got them from this site. If not then there may be some you missed.
0
 
gabrielPennybackAuthor Commented:
Thanks! BTW, I know I can create a public declaration of variables accessible in all subs in a given module, but is there a way to define all the color variables in one module such that they will be accessible by all other modules?

- John
0
 
andrewssd3Commented:
If you declare them Public, they will be accessible to all the modules in the VBA project.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now