If statement based on cell color?

Alagan

New Member
Joined
May 20, 2008
Messages
9
Hi,

"IF" the cell color in the next column is a particular color.

I would like to return a value say 1,2,3..etc.. for each color.


Does anyone know how to write an if statement based on this? :confused:
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You will have to use VBA.

For example:
Code:
If Range("a1").Interior.ColorIndex = 3 Then
     ' your code
End If
 
Upvote 0
Hi Alagan,

I have purely used this to check on cells E19:E20 but is easily altered;

Code:
For i = 19 To 20
    If Cells(i, 5).Interior.ColorIndex = 3 Then
      Cells(i, 6) = 1
        Else
            If Cells(i, 5).Interior.ColorIndex = 6 Then
                Cells(i, 6) = 2
            End If
    End If
Next i

the for i is the range of rows you are checking with 19 To 20 being row numbers.

Cells(i , 6) equates to Cells(Row Number , Column Number)

The colurIndex identifys colour, if you record a macro of you colouring cells it will give you a fair idea of what numbers you require for your colour range.

Hope this helps.
 
Upvote 0
Hi, you have to indicate what you are talking about when you write, "cell color".

Are we talking about the font? Or are we talking about cell shading with regards to pattern: FORMAT/CELL/[PATTERN TAB]

If we are talking about cell shading, then there is no direct Excel function that will allow you to determine the color.

However, creating a custom function is quite easy. Put the following in a module in your worksheet:

Function WhatColor(rngA As Range) As Integer
WhatColor = rngA.Interior.ColorIndex
End Function

Your IF statement on your worksheet would then be =IF(WhatColor(A1)=54,"purple","not purple")

You'll find though that if you change the color of the cell pattern, your spreadsheet will not calculate. Thus the function WhatColor does not update. Even if you declare the function volatile. It will only update when you actually change a value in any cell in the workbook.

This may not fit what you need and you may want to think of an alternative way to do what you want.

Good luck.
 
Upvote 0
The reason I am doing this, is so that I can then filter on these...

As I am finding it quite difficult to program in VBA based on a color filter.

I will try and adapt on the code - Thank you! :cool:
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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