ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Determining Target font colour (https://www.excelbanter.com/excel-programming/416458-determining-target-font-colour.html)

kirkm[_7_]

Determining Target font colour
 

I'm wanting to detect if the font colour in the selected cell is black
or yellow.
Been playing with target. and the drop down items but can't see
anything (yet).

Any help appreciated.
Thanks - Kirk

JLGWhiz

Determining Target font colour
 
If the cell interior is filled by standard format method, then:

Private Sub Worksheet_Change(ByVal Target As Range)
Set myRange = Sheets(1).Range("A1")
If Target = myRange Then
MsgBox myRange.Interior.ColorIndex
End If
End Sub

This next one is a little tricky. It basically does the same thing
but it only returns the setting for the conditional format and
does not tell you if the condition is met. That means that if
the condition is not met, the cell will not be color coded.

Private Sub Worksheet_Change(ByVal Target As Range)
Set myRange = Sheets(1).Range("A1")
If Target = myRange Then
MsgBox myRange.FormatConditions(1).Interior.ColorIndex
End If
End Sub


"kirkm" wrote:


I'm wanting to detect if the font colour in the selected cell is black
or yellow.
Been playing with target. and the drop down items but can't see
anything (yet).

Any help appreciated.
Thanks - Kirk


JLGWhiz

Determining Target font colour
 
P.S. The code goes behind the worksheet. Right click the sheet tab and click
View Code in the drop down menu. Then paste it in.

"kirkm" wrote:


I'm wanting to detect if the font colour in the selected cell is black
or yellow.
Been playing with target. and the drop down items but can't see
anything (yet).

Any help appreciated.
Thanks - Kirk


RyanH

Determining Target font colour
 
You can do this a few ways depending on what you prefer.

1.) Put this code in Worksheet Module or the worksheet you wish to test. I
think this may get a bit annoying but it will do the trick.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Select Case Target.Font.ColorIndex
Case Is = 1
MsgBox "The Font ColorIndex = 1 or Black"
Case Is = 6
MsgBox "The Font ColorIndex = 6 or Yellow"
End Select

End Sub

2.) You can call a user defined function from a cell, which will return
Black or Yellow.

Public Function DetectFontColor(CellToDetect As Range) As String

Select Case CellToDetect.Font.ColorIndex
Case Is = 1
DetectFontColor = "Black"
Case Is = 6
DetectFontColor = "Yellow"
End Select

End Function

3.) You could use the immediate window in the VB Editor. Just select a cell,
then in VBE select ViewImmediate Window then type this:

?ActiveCell.Font.ColorIndex

This will give you the colorindex number.

I hope one of these helped! If so, please let me know and click "YES" below.
--
Cheers,
Ryan


"kirkm" wrote:


I'm wanting to detect if the font colour in the selected cell is black
or yellow.
Been playing with target. and the drop down items but can't see
anything (yet).

Any help appreciated.
Thanks - Kirk



All times are GMT +1. The time now is 06:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com