VBA checking colour macro

  • Thread starter Thread starter Legacy 93538
  • Start date Start date
L

Legacy 93538

Guest
Hi

Does anyone know how to write an if statement which checks the colour of a cell range?

I ahve a loop which loops through a cell range but its not checking the right colour. I know RGA codes for the colour i want it to check but i am not sure how to use RGA in this loop.

Code:
    If Selection.Interior.Color = 65535 Then
        Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "True"
    Else: Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "False"
    End If

Can anyone help me?

Thanks

Jessicaseymour
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
It would be like this (substitute the correct RGB colour

Code:
Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = Selection.Interior.Color = RGB(255, 255, 0)
 
Upvote 0
Hi,

Try this:

Code:
    lRow = Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lRow
        If Cells(i, 1).Interior.ColorIndex = 10 Then
            Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "True"
        Else
            Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "False"
        End If
    Next
 
Upvote 0
Hi

Thanks! but its still not checking as its not returning false when the statement says if the cell is this colour place false in the cell but its not have i wrirtten it wrong? This is full loop which consists of 3 if statements the last two are the colour if statements which are not returning the correct results.

Code:
For Each cell In Sheets("PPIIIFORM").Range("F4:U644")
    If cell.Value <> "" Then
        Sheets("Input_Reference_Table").Cells(Nrow, 1).Value = cell.Value
    End If
    If Selection.Interior.ColorIndex = RGB(217, 217, 217) Then
        Sheets("Input_Reference_Table").Cells(Nrow, 12).Value = "False"
    Else: Sheets("Input_Reference_Table").Cells(Nrow, 12).Value = "True"
    End If
    If Selection.Interior.ColorIndex = RGB(255, 255, 0) Then
        Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "True"
    Else: Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = "False"
    End If
    Nrow = Nrow + 1
    Application.StatusBar = cell
Next cell
 
Upvote 0
Try

Code:
For Each cell In Sheets("PPIIIFORM").Range("F4:U644")
    If cell.Value <> "" Then Sheets("Input_Reference_Table").Cells(Nrow, 1).Value = cell.Value
    Sheets("Input_Reference_Table").Cells(Nrow, 12).Value = Not cell.Interior.Color = RGB(217, 217, 217)
    Sheets("Input_Reference_Table").Cells(Nrow, 14).Value = cell.Interior.Color = RGB(255, 255, 0)
    Nrow = Nrow + 1
    Application.StatusBar = cell.Address
Next cell
 
Upvote 0
Hi

Ok i tried changing "Selection" to "Cell" but still did not worked.

VoG - ok but i need to include the two else statements to say if the colour is not put cell in Input_Reference_Table to True for first one and False ofr the second one
 
Upvote 0
VoG - ok but i need to include the two else statements to say if the colour is not put cell in Input_Reference_Table to True for first one and False ofr the second one

Did you try it? You don't need an If/Else structure for that type of comparison where you need to return True or False. The expressions that I wrote evaluate to True or False.
 
Upvote 0
Tried works perfectly thanks so much for help!! Sorry when i read i got a little confused sorry!

Thanks everyone for all your help!!! Its greatly appricated
 
Upvote 0
Hi

Sorry about this but would anyone know how to create an if statement which checks the number formatting of a cell?

AS i need to check a cell and:

if it is a percentage with no decimal in Sheets("Input_Reference_Table").Cells value should be %10.0%%

if it is a percentage with a decimal in Sheets("Input_Reference_Table").Cells value should be %10.(whatever the decmial is)%%

if it is a number with no decimal in Sheets("Input_Reference_Table").Cells value should be %10.0%

if it is a number with a decimal in Sheets("Input_Reference_Table").Cells value should be %10.(whatever the decmial is)%

can anyone help?
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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