Question on Cell Color Based On Input

tunacan

New Member
Joined
Nov 22, 2010
Messages
3
Hello,

I am an Excel newbie and have exhausted what detail I could find on the web to help me debug this problem.

The code below works if you type the word 'ID' or 'Severity' in a cell, it changes to Purple with a yellow-ish font. It only works if you paste in the word 'ID' or 'Severity' etc.. in a cell after double-clicking that cell. Any thoughts ?

I populate columns A:L with a web query and am trying to Highlight the header row. The code I am refering to starts with 'Insert Color Hack' but I included the entire snippet which allows me to add multiple values per cell using a drop-down list.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim icolor As Integer
Dim fcolor As Integer
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column >= 1 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & vbCrLf & newVal
End If
End If
End If
End If

' Insert Color Hack
If Not Intersect(Target, Range("A:L")) Is Nothing Then

Select Case Target
Case "ID", "Service", "Status", "Severity", "Resolution", "Description"
icolor = 21
fcolor = 45
Case Else
'Whatever
End Select
Target.Interior.ColorIndex = icolor
Target.Font.ColorIndex = fcolor
End If
' End Color Hack
exitHandler:
Application.EnableEvents = True
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Can I ask why you are using VBA code an not using Conditional Formatting for this?

  • Select cell A1
  • Select columns A to L
  • Select from the menu Format\ Conditional Formatting
    • Condition 1:
    • Formula is =OR(A1="ID",A1="Service",A1="Status",A1="Severity",A1="Resolution",A1="Description")
    • Format your background color and font color
    • OK
 
Upvote 0
Thanks so much, that worked perfectly and I am extremely grateful for your help with this.

I knew that you could use FORMULA's with conditional formatting but was not aware of the syntax with the =OR statement.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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