How return built-in constant value from its name

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,807
Cell A1 contains the string: rgbAliceBlue

rgbAliceBlue is a built-in VBA constant with the numeric value of 16775408

In the VBE Immediate window if I enter ?rgbAliceBlue it shows the value: 16775408

Application.Evaluate("rgbAliceBlue") returns Error 2029 - Type mismatch

How can I extract the numeric value of a built-in excel constant using the name of a built-in excel constant?
 

Some videos you may like

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,626
Office Version
365
Platform
Windows
How about
Code:
val(rgbAliceBlue)
 

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,807
val(rgbAliceBlue) does return the numeric value, but when I retrieve the value of a text string in Range("A1") it is processed as a string and Val("rgbAliceBlue") returns 0

I want to retrieve the name of a color from A1

A1 = rgbAliceBlue

which is a text value

In code
x = Range("A1") .Value
or
x = Range("A1") .Value2
or
x = Range("A1") .Text

all result in x having the value of "rgbAliceBlue" (a string)

and Val(X) returns 0, not 16775408 (the value of the constant: rgbAliceBlue)

Is there a function (or code) that will return the numeric value of a built-in constant using its name as input?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,626
Office Version
365
Platform
Windows
I missed the part about it being in A1.
And have no idea how to convert a string to a constant
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,906
Office Version
2010
Platform
Windows
I am pretty sure you cannot retrieve the value directly as, to my understanding, all constants are replaced by their value at the time the code is compiled (which is before the code itself physically executes), so the running VB code has no knowledge that a constant name was used originally and, hence, does not know that rgbAliceBlue is the name of a constant nor that it has an associated value.
 

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,807
Thank you both for your suggestions and comments.

I had thought there might be some way to use the functionality of the immediate window since it returns numeric values when evaluating the built-in constants.
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,099,018
Messages
5,466,066
Members
406,462
Latest member
I10V3xl

This Week's Hot Topics

Top