Hi All,
I'm designing a holiday chart in work, and am using conditional formatting to colour cells.
The problem is I have more than 3 conditions. Consequently I need to use a macro.
I have the following code, but have a couple of questions.
My range select isn't working as I'd like? I need to select 2 individual ranges at the same time?Private Sub Worksheet_Change(ByVal Target As Range) Dim icolor As Integer If Not Intersect(Target, Range("G8:GE30", "G35:GH57")) Is Nothing Then Select Case Target Case 0 To 1 icolor = 6 Case Else 'Whatever End Select Target.Interior.ColorIndex = icolor End If End Sub
G8:GE30 and G35:GH57 but based on what is entered above excel seems to be selecting G8:GH57? Can anyone tell me how to fix this?
Also
Can anyone advise how to include a second condition e.g. if the letter L is inserted in a cell, i'd like to shade it grey?
I'd appreciate any help!
Many thanks,
John![]()
Hey, you can Union the 2 ranges together like so:
Private Sub Worksheet_Change(ByVal Target As Range) Dim icolor As Integer If Not Intersect(Target, Union(Range("G8:GE30"), Range("G35:GH57"))) Is Nothing Then Select Case Target.Value Case 0 To 1 icolor = 6 Case Else 'Whatever End Select Target.Interior.ColorIndex = icolor End If End Sub
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
Thanks for that, but can anyone help with conditional formatting for letters also?
ie If I enter a letter I'd also like to shade the cell a particular colour?
Simply amend the Select Case as required.
Private Sub Worksheet_Change(ByVal Target As Range) Dim icolor As Integer If Not Intersect(Target, Union(Range("G8:GE30"), Range("G35:GH57"))) Is Nothing Then Select Case Target.Value Case 0 To 1 icolor = 6 Case "A" icolor = 3 Case Else 'Whatever End Select Target.Interior.ColorIndex = icolor End If End Sub
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Hi,
I've got it to work was the suggested range and also the conditional formatting with the lettering now works.
Strangely I have to put the case for the lettering before the case for the numbering, otherwise it doesn't function.
Anyway the following code now works, so thanks for your help!
ThanksPrivate Sub Worksheet_Change(ByVal Target As Range) Dim icolor As Integer If Not Intersect(Target, Union(Range("G8:GE30"), Range("G35:GH57"))) Is Nothing Then Select Case Target.Value Case "L" icolor = 16 Case "l" icolor = 16 Case "B" icolor = 5 Case "b" icolor = 5 Case 0 To 1 icolor = 36 Case Else 'Whatever End Select Target.Interior.ColorIndex = icolor End If End Sub
Last edited by Jay3; 11-10-2008 at 04:51 PM. Reason: Tagging
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks