VB Code Question

  • Thread starter Thread starter Stan
  • Start date Start date
S

Stan

Can someone help me by telling me how I can refer to a cell's color and the
perform an action? In other words, if a cell is red, then docmd.xxxxx?

Many thanks!
 
Sub dural()
If ActiveCell.Interior.ColorIndex = 3 Then
MsgBox ("the active cell is red")
Else
MsgBox ("the active cell is not red")
End If
End Sub
 
Many thanks for the quick reply.

When I copy the code into Visual Basic then color code a cell, I don't
recieve the MsgBox so I must be doing something wrong. Do you have to refer
to the particular workbook or worksheet in the code?
 
Sub do_something()
If ActiveCell.Interior.ColorIndex = 3 Then
MsgBox "What now?" 'run a macro or do something
Else
MsgBox "Not Red!" 'do nothing and exit
End If
End Sub


Gord Dibben MS Excel MVP
 
Works Great! Do you know how I can tell what the ColorIndex is for each color?

I see that 3 = Red but I also need Green, Blue, Orange, and Yellow.
 
Run this macro.

Sub ListColorIndexes()
Dim Ndx As Long
Sheets.Add
For Ndx = 1 To 56
Cells(Ndx, 1).Interior.ColorIndex = Ndx
Cells(Ndx, 2).Value = Hex(ThisWorkbook.Colors(Ndx))
Cells(Ndx, 3).Value = Ndx
Next Ndx
End Sub


Gord
 
Back
Top