Changing color of a cell based on each change in cell value (not a conditional format)

sanilmathews

Board Regular
Joined
Jun 28, 2011
Messages
102
Hi All,

I would need a code for the below requirement on Worksheet_Change with each change in an ActiveCell.

A random cell (say A2) would be blank and also interior color as "No fill" initially. When there is first data input in cell A2, the cell color would change to yellow. For each instance of input the color would change for the cell A2. With this I could identify on how many instances of input was done on the same cell (i.e. A2) by a user using color code.

Initial = No fill
First input = Green
Second Input = Yellow
Third Input = Orange
Fourth Input and beyond = Red

I have the below code which is actually not giving what is expected. However I would appreciate if I can get a code that actually meets the above requirement.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Interior.ColorIndex = -4142 Then ActiveCell.Interior.ColorIndex = 43
If ActiveCell.Interior.ColorIndex = 43 Then ActiveCell.Interior.ColorIndex = 6
If ActiveCell.Interior.ColorIndex = 6 Then ActiveCell.Interior.ColorIndex = 44
If ActiveCell.Interior.ColorIndex = 44 Then ActiveCell.Interior.ColorIndex = 22
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
      With Target.Interior
         Select Case .ColorIndex
            Case -4142: .ColorIndex = 43
            Case 43: .ColorIndex = 6
            Case 6: .ColorIndex = 44
            Case Else: .ColorIndex = 22
         End Select
      End With
   End If
End Sub
this works on A2:A100, but you can change the range to suit
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
      With Target.Interior
         Select Case .ColorIndex
            Case -4142: .ColorIndex = 43
            Case 43: .ColorIndex = 6
            Case 6: .ColorIndex = 44
            Case Else: .ColorIndex = 22
         End Select
      End With
   End If
End Sub
this works on A2:A100, but you can change the range to
Thank you for your response. I actually not needed to define any range. It should be executing on any given ActiveCell.
 
Upvote 0
Thank you for your response. I actually not needed to define any range. It should be executing on any given ActiveCell.
Then just remove the range "IF" clause from Fluff's code, i.e.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub

         With Target.Interior
         Select Case .ColorIndex
            Case -4142: .ColorIndex = 43
            Case 43: .ColorIndex = 6
            Case 6: .ColorIndex = 44
            Case Else: .ColorIndex = 22
         End Select
      End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,234
Members
448,951
Latest member
jennlynn

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