ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Font/CellInteriorColour (https://www.excelbanter.com/excel-programming/325840-font-cellinteriorcolour.html)

ExcelMonkey[_190_]

Font/CellInteriorColour
 
I am comparing font colour with cell interior colour.
When I colour the font black and use a black interior,
the function returns a true value. When I colour the font
white, against a white cell interior, I get an error.
Should I be using color.index instead? Or am I running
into a problem with one of the colours being
xlColorIndexAutomatic. If so, How do I ensure that the
comparison compares like numeric values to create the
correct boolean value for the funtion?

Public Function CellHasSameFontAndInteriorColour(rng As
Range)
Dim FontColour As Integer
Dim InteriorColour As Integer

With rng
FontColour = .Font.Color
InteriorColour = .Interior.Color

If FontColour = InteriorColour Then
CellHasSameFontAndInteriorColour = True
Else
CellHasSameFontAndInteriorColour = False
End If
End With

End Function

Bob Phillips[_6_]

Font/CellInteriorColour
 
Your variables need to be Longs.

But you can simplify it without variables

Public Function CellHasSameFontAndInteriorColour(rng As Range)
With rng
CellHasSameFontAndInteriorColour = _
.Font.Color = .Interior.Color
End With
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote in message
...
I am comparing font colour with cell interior colour.
When I colour the font black and use a black interior,
the function returns a true value. When I colour the font
white, against a white cell interior, I get an error.
Should I be using color.index instead? Or am I running
into a problem with one of the colours being
xlColorIndexAutomatic. If so, How do I ensure that the
comparison compares like numeric values to create the
correct boolean value for the funtion?

Public Function CellHasSameFontAndInteriorColour(rng As
Range)
Dim FontColour As Integer
Dim InteriorColour As Integer

With rng
FontColour = .Font.Color
InteriorColour = .Interior.Color

If FontColour = InteriorColour Then
CellHasSameFontAndInteriorColour = True
Else
CellHasSameFontAndInteriorColour = False
End If
End With

End Function




K Dales[_2_]

Font/CellInteriorColour
 
I think your problem comes from the fact that the .Color property is a long
integer value; black is zero so that works but white is out of bounds for an
integer - try a Double instead

"ExcelMonkey" wrote:

I am comparing font colour with cell interior colour.
When I colour the font black and use a black interior,
the function returns a true value. When I colour the font
white, against a white cell interior, I get an error.
Should I be using color.index instead? Or am I running
into a problem with one of the colours being
xlColorIndexAutomatic. If so, How do I ensure that the
comparison compares like numeric values to create the
correct boolean value for the funtion?

Public Function CellHasSameFontAndInteriorColour(rng As
Range)
Dim FontColour As Integer
Dim InteriorColour As Integer

With rng
FontColour = .Font.Color
InteriorColour = .Interior.Color

If FontColour = InteriorColour Then
CellHasSameFontAndInteriorColour = True
Else
CellHasSameFontAndInteriorColour = False
End If
End With

End Function


ExcelMonkey[_190_]

Font/CellInteriorColour
 
Thank-you once again. I may have to start paying you guys
for this education!

Kind Regards
EM


-----Original Message-----
Your variables need to be Longs.

But you can simplify it without variables

Public Function CellHasSameFontAndInteriorColour(rng As

Range)
With rng
CellHasSameFontAndInteriorColour = _
.Font.Color = .Interior.Color
End With
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"ExcelMonkey" wrote

in message
...
I am comparing font colour with cell interior colour.
When I colour the font black and use a black interior,
the function returns a true value. When I colour the

font
white, against a white cell interior, I get an error.
Should I be using color.index instead? Or am I running
into a problem with one of the colours being
xlColorIndexAutomatic. If so, How do I ensure that the
comparison compares like numeric values to create the
correct boolean value for the funtion?

Public Function CellHasSameFontAndInteriorColour(rng As
Range)
Dim FontColour As Integer
Dim InteriorColour As Integer

With rng
FontColour = .Font.Color
InteriorColour = .Interior.Color

If FontColour = InteriorColour Then
CellHasSameFontAndInteriorColour = True
Else
CellHasSameFontAndInteriorColour = False
End If
End With

End Function



.



All times are GMT +1. The time now is 08:32 AM.

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