Excel Color Functions

AlexCS

Board Regular
Joined
Oct 31, 2011
Messages
78
Hi everyone,

I need one of my cells to take a certain value based on another cell's background color that is assigned via conditional formatting. Finding a function that returns the colorindex of a cell was relatively easy - the problem is that this function returns -4142 when it comes accross conditionally formatted cells (which is the code for no fill), although the visible colorindex for that cell is 42.

Does anyone know how to make the below function work on cells colored via conditional formatting?

Many thanks,

Alex

Function ColorIndexOfOneCell(Cell As Range, OfText As Boolean) As Long

Dim CI As Long

Application.Volatile True
If OfText = True Then
CI = Cell(1, 1).Font.ColorIndex
ColorIndexOfOneCell = CI
Else
CI = Cell(1, 1).Interior.ColorIndex
ColorIndexOfOneCell = CI
End If

End Function
 
No, I was referring to the more recent link.

Your code exits on the first True result, but what if that rule doesn't have any interior formatting, but a later one, also true, does?

I think changing the Evaluate to Cell.Worksheet.Evaluate does resolve some of the other issues (and eliminates the need to select the cell), and would allow it to work as a UDF and reference across sheets.
 
Last edited:
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Your code exits on the first True result, but what if that rule doesn't have any interior formatting, but a later one, also true, does?
Good point!!! I had completely overlooked that possibility/probability.:eek:

I think changing the Evaluate to Cell.Worksheet.Evaluate does resolve some of the other issues (and eliminates the need to select the cell), and would allow it to work as a UDF and reference across sheets.
I'll play around with that idea. Thanks for mentioning it.
 
Upvote 0
What seems more difficult is knowing whether a format includes fill -- at a glance, I don't see anything in the object model that tells you.
 
Upvote 0
Thanks Rick and shg for not giving up on this after I had given up myself since I hardly have to deal with such complications.

After reading the explanations in your last posts, I have a much better understanding of how the code works :) thanks again!

Alex
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,280
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top