|
Determine a Cell's Color with this UDF - Outputs as Text or the Index Number in Excel
This free Excel UDF allows you to output the color of a cell in text format or as that color's index number. Also note that this refers to the fill color of a cell. For instance, if you point the function to a cell that is red, the function will ouput "Red" or the index number "3." The range argument for this function just points to the cell that has color that you want to identify. By defualt, the function outputs the color's name as text. If you set the true/false argument to true, the function outputs the color as an index number.
This UDF function will not display all of the colors in Excel since Excel 2007 has a very large amount of available colors. If a color cannot be identified by this UDF, it will output default text that let's you know that it cannot identify the color.
Where to install the macro: Module
UDF to Determine a Cell's Color and Output that Color as Text or an Index Number in Excel
Function CELLCOLOR(rCell As Range, Optional ColorName As Boolean)
Dim strColor As String
Dim iIndexNum As Integer
Select Case rCell.Interior.ColorIndex
Case 1
strColor = "Black"
iIndexNum = 1
Case 53
strColor = "Brown"
iIndexNum = 53
Case 52
strColor = "Olive Green"
iIndexNum = 52
Case 51
strColor = "Dark Green"
iIndexNum = 51
Case 49
strColor = "Dark Teal"
iIndexNum = 49
Case 11
strColor = "Dark Blue"
iIndexNum = 11
Case 55
strColor = "Indigo"
iIndexNum = 55
Case 56
strColor = "Gray-80%"
iIndexNum = 56
Case 9
strColor = "Dark Red"
iIndexNum = 9
Case 46
strColor = "Orange"
iIndexNum = 46
Case 12
strColor = "Dark Yellow"
iIndexNum = 12
Case 10
strColor = "Green"
iIndexNum = 10
Case 14
strColor = "Teal"
iIndexNum = 14
Case 5
strColor = "Blue"
iIndexNum = 5
Case 47
strColor = "Blue-Gray"
iIndexNum = 47
Case 16
strColor = "Gray-50%"
iIndexNum = 16
Case 3
strColor = "Red"
iIndexNum = 3
Case 45
strColor = "Light Orange"
iIndexNum = 45
Case 43
strColor = "Lime"
iIndexNum = 43
Case 50
strColor = "Sea Green"
iIndexNum = 50
Case 42
strColor = "Aqua"
iIndexNum = 42
Case 41
strColor = "Light Blue"
iIndexNum = 41
Case 13
strColor = "Violet"
iIndexNum = 13
Case 48
strColor = "Gray-40%"
iIndexNum = 48
Case 7
strColor = "Pink"
iIndexNum = 7
Case 44
strColor = "Gold"
iIndexNum = 44
Case 6
strColor = "Yellow"
iIndexNum = 6
Case 4
strColor = "Bright Green"
iIndexNum = 4
Case 8
strColor = "Turqoise"
iIndexNum = 8
Case 33
strColor = "Sky Blue"
iIndexNum = 33
Case 54
strColor = "Plum"
iIndexNum = 54
Case 15
strColor = "Gray-25%"
iIndexNum = 15
Case 38
strColor = "Rose"
iIndexNum = 38
Case 40
strColor = "Tan"
iIndexNum = 40
Case 36
strColor = "Light Yellow"
iIndexNum = 36
Case 35
strColor = "Light Green"
iIndexNum = 35
Case 34
strColor = "Light Turqoise"
iIndexNum = 34
Case 37
strColor = "Pale Blue"
iIndexNum = 37
Case 39
strColor = "Lavendar"
iIndexNum = 39
Case 2
strColor = "White"
iIndexNum = 2
Case Else
strColor = "Custom color or no fill"
End Select
If ColorName = False Or strColor = "Custom color or no fill" Then
CELLCOLOR = strColor
Else
CELLCOLOR = iIndexNum
End If
End Function
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
Yes, likely a dumb question, but that's about my corresponding skill set thus far...
How do I determine a color index #?
I've got a big nasty macro that I'm not looking to re-record/build. I just need to change some of the colors used.
?#1:So, in the following portion of script, all I would need to do is change the # listed beside "Color Index = " correct?
?#2:Is there a listing of colors and their corresponding color index #s? Or a way to determine the #?
Sub Format_Lifecycle_color_PD()
On Error GoTo Format_Lifecycle_color_Err
'Loop for PD
With Application.FindFormat.Font
.FontStyle = "Bold"
.Superscript = False
.Subscript = False
.ColorIndex = xlAutomatic
End With
Application.FindFormat.Interior.ColorIndex = xlNone
Do While Cells.Find(What:="PD", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True).Activate
With Selection.Interior
.ColorIndex = 34 .Pattern = xlSolid
End With
Selection.Font.ColorIndex = 0
Loop
Format_Lifecycle_color_Exit:
Exit Sub
Format_Lifecycle_color_Err:
Resume Format_Lifecycle_color_Exit
End Sub
I have found several macros and udf's on this site and others that will return the color index number of a cell's background. But they seem to be limited as several of the available colors in Excel 2007 will return the same number. Does anybody have a way of finding a more accurate and complete list of color index numbers for Excel 2007? Thanks for your help.
I'm trying to set an integer variable = to the Column.Index number for a particular cell in a given table. The row x column location of the selected cell is NOT however known to the macro. The column object seems to only be available under the Cell(RxC) parent, but the Cell(RxC) is unknown in my case.
I have several tables in a DOC that have a merged cells in the bottom row(s). The number of these merged cell rows is not set. In the macro, I am moving around in the table doing various things, and at some point I need to know what column I'm in. And, due to the merged cells at the bottom, the Cell(RxC) position can get lost.
So, I was hoping that the Column.Index property would give me my location, but I can't seem to determine the correct code structure. HELP!
The following is close, but w/o the Cell(RxC) it won't compile.
Code:
Column_Num = Selection.Table(1).Range.Cell().Column.Index
Hi,
needed some help. I needed to manage the progrees on project and tasks of my team members. In the input sheet, I enter the daily, weekly, monthly and quarterly tasks with the name of person, start and end date and description of the task.
In the oupt sheet, I intend to have a sheet for each person and wish to compile their quarterly, monthly and weekly tasks with their start and end dates.
I tried to solve the first part of the problem by trying to get the Quarterly or Q tasks for Ajay. I tried using the index function in the same syntax.
INDEX(D1:D100,MATCH(1,(A1:A100="x")*(B1:B100="y")*(C1:C100="z"),0)).
What I observed that the output would show only the first the Q task and not remaining.
Please advise.
The input sheet look like this
Row/column
A
B
C
D
E
F
1
Month
Date
Q?M?W/d
person
narrative
end date
2
Jan
01-01-2010
Q
Ajay
this first
10-10-2010
3
Jan
02-01-2010
Q
Shyam
xyz
10-10-2010
4
Jan
03-01-2010
Q
Ajay
abc
10-10-2010
5
Jan
04-01-2010
M
Ajay
sdas
11-10-2010
6
Jan
05-01-2010
M
Ajay
ds
12-10-2010
7
Jan
06-01-2010
M
Shyam
sdas
13-10-2010
8
Jan
07-01-2010
D
Ajay
dsd
14-10-2010
9
Jan
08-01-2010
D
Ajay
sds
15-10-2010
The out Put Sheet for each person
A
B
C
D
E
F
G
H
I
J
K
1
List of Quarterly
List of Monthly
2
person
Task
Start date
End date
narrative
person
Task
Start date
End date
narrative
3
Ajay
Q
Ajay
M
I am editing code that changes a cell's forecolor and I can't seem to find the number for red. What number is red, please?
--DJ
I'm doing some work in VBA and was wondering where I could find a list of the number indexes associated with each color found on the basic Excel color pallette.
How can I interrogate the contents of a cell to determine whether or not it contains a number formatted as text? I intend to run a .value = .value over each such cell to convert it to a number format, but I only want to do this to relevant cells.
I need a worksheet formula to determine the next value in a series that contains both a letter and number. For example, Col A consists of P10101, P10102, P10103, etc. Col B is a description of the proposed work represented by Col A. When I enter something in the next open Col B, I want Col A to show P10104. The tricky part is that users can sort the file so Col A is no longer "in order", and there may be hundreds of P-designations so it is not always this obvious what the next value should be. Is there a formula I can use in Col A to strip the numeric part out of all of the previous entries, find the max value and increment it, then add back the "P"?
Thanks in advance for help!
Hi All
I have a column which contains an inventory item's specification which comes from the system.
The spec may or may not be divided by spaces:
C981: -OR- C981:
1260mmx50m 1260 mm x 50 m
Basically I am trying to extract the "50" only and the below forms an extract of the problem part of the formula.
I am using the following formula, making use of the "x" to split the string:
=IF(ISNUMBER(MID(C981,(SEARCH("x",C981)+1),1)),"1","2")
If I use f(x) to see the values that each formula is returning, "ISNUMBER" returns the "5" and "" correctly in both instances, however the IF function returns false on both occassions as well. I have no idea why??
I hope I have explained this clearly...
Hi,
I need to return one value if the very last character in a cell containing text is a number (ie the characters 0 thru 9), otherwise return a different value.
I've tried all sorts of strong parsing methods, but almost always get an error.
Could someone please assist ?
Thanks so much !!
Steve C.
|
|