I have a spreadsheet in excel where there are three types of cell. Black cells, yellow cells and cells with no fill. I am trying to write code so that the black cells will contain value 1
, the yellow cells value 2
and the no fill cells value 0
.
So far, this is what I have for the black and yellow cells:
Sub changeValuesBasedOnColour()
Dim rg As Range
Dim xRg As Range
Set xRg = Selection.Cells
Application.DisplayAlerts = False
For Each rg In xRg
With rg
Select Case .Interior.Color
Case Is = 0 'Black
.Value = 1
Case Is = 255255 'Yellow
.Value = 2
End Select
End With
Next
Application.DisplayAlerts = False
End Sub
This has worked for the cells in my spreadsheet which are filled black: they all now contain the value 1
. However, nothing has changed for my cells filled yellow.
I thought that it could be to do with the wrong HEX code, but I have tried 2552550
and ```255255000`` as well. The yellow cells are filled with the yellow from the excel standard colors, as seen below.