Background colour of cells to change based on value.

KyleJackMorrison

Board Regular
Joined
Dec 3, 2013
Messages
107
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello,

I have a document (shared) which we use as a calendar and to keep track of people and where they are. It has all 365 day on it and with over 100 people.
I would like a vba to search the document and change the cell colour based on the value. For example: Red if someone is away on holiday. Green if they are on a course.

I use conditional formatting however as people copy and paste in the document, it copies the conditional formation stuff which now has rendered the document unworkable as it crashes due to too many conditional formatting rules.

I have also tried a simple code which colours the cells based on a value, however if someone types "holiday" or "HOLIDAY" it wont change the colour due to the code only searching for "Holiday" in ProperCase.

Is there a more efficient way of having a code where i don't have to put in every type of case search criteria which will slow down the sheet.

Many thanks in advance!

(Current code im using)
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myRange As Range
Dim myCell As Range
Set myRange = Range("G5:NL104")
    For Each myCell In myRange
    If InStr(1, (myCell.Value), "Leave") > 0 Then
    myCell.Interior.ColorIndex = 6
    End If
    For Each myCell In myRange
    If InStr(1, (myCell.Value), "LEAVE") > 0 Then
    myCell.Interior.ColorIndex = 6
    End If
    For Each myCell In myRange
    If InStr(1, (myCell.Value), "leave") > 0 Then
    myCell.Interior.ColorIndex = 6
    End If
    For Each myCell In myRange
    If InStr(1, (myCell.Value), "Holiday") > 0 Then
    myCell.Interior.ColorIndex = 6
    End If
    For Each myCell In myRange
    If InStr(1, (myCell.Value), "HOLIDAY") > 0 Then
    myCell.Interior.ColorIndex = 6
    End If
    For Each myCell In myRange
    If InStr(1, (myCell.Value), "holiday") > 0 Then
    myCell.Interior.ColorIndex = 6
    End If




Next myCell
End Sub
 
I had a selectionchange event on another sheet which i removed, and put in that new code that you've provided.

Thanks for this, unfortunately i have to use merged cells as my work uses this sheet as a calendar of events and merge the cells with relevant info.
But for now it is working to perfect. I'll even leave that error handling message in aha!

Cheers @My Aswer Is This
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Well you may need another feature you did not ask for.

If you enter leave for example and the cell color turns Red but then you change your mine and want to delete the value or change the value to some other value not one of the ones in your choice of words like you enter Take a hike George

Then with this new script the cell color will turn back to no color.

This case Else

Must be the last one

It means if you enter any other value the cell color will be nothing
Code:
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/16/2018  5:25:19 PM  EDT
If Not Intersect(Target, Range("I6:NN105")) Is Nothing Then
On Error GoTo M
If Target.Cells.CountLarge > 1 Then Exit Sub
With Target.Interior
Select Case Target.Value
Case "Leave", "Holiday"
.ColorIndex = 3
Case "Me", "You", "Us", "They"
.ColorIndex = 8

Case "Her", "Him", "Them"
.ColorIndex = 5
Case Else
.ColorIndex = xlNone

End Select
End With
Exit Sub
M:
MsgBox "Bad Boy using merged cells again?"
End If

End Sub
 
Last edited:
Upvote 0
You are a gift that just keeps on giving! Thanks. :)

Also, what if someone in my sheet was to enter in "Summer Holiday". The script wont change as it's only recognising "Holiday". Is there a way to search and change words containing "holiday"?
 
Upvote 0
No I really do not know how to do that using Case.

That's why you may need Data Validation lists for those ranges.

It restricts what users can enter.

Like they can only enter Leave or Vacation or such

I assume you know what a Data Validation list is.
 
Upvote 0
No worries you've done alot to help me out.

Yeah i use that on a different part of the workbook. I'll just put obvious things in that people might enter if i don't use data validation.

Many thanks :D
 
Upvote 0
Or with the case statemet enter

"Leave","Summer Leave",Winter Leave"

But not possible with just any thing the user may enter.

Like:
I want my Leave on any day but the day your on leave.


But I
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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