Highlighting cells on specific words

Hi all, a little stuck here and think i might have part but not all the answer.
I have cell which are populated with a department name, IE Maths / English / History blah…
I would like to change the background colour of that cell depending on the department. Here is what i have so far.

if($dept == "") {$deptcolour = "White";}
if($dept == "No") {$deptcolour = "Red";}
if($dept == "SEN") {$deptcolour = "Yellow";}
if($dept == "Admin") {$deptcolour = "Orange";}
if($dept == "Science") {$deptcolour = "Green";}
if($dept == "Maths") {$deptcolour = "blue";}

It seem to be partly working but then does highlight other cell that are not specified. Top one left blank for unselected department.

Instead of a plethora of if-statements you should use a switch statement, see the link below for a reference.

In your case you will want to change your code to something like this (to begin with):

case “No”:
$deptcolour = “Yellow”;
case “SEN”:
$deptcolour = “Red”;
case “Admin”:
$deptcolour = “Orange”;
case “Science”:
$deptcolour = “Green”;
case “Maths”:
$deptcolour = “blue”;
$deptcolour = “White”;

It’s easy to understand and very readable/scalable. Have a look at that.

Hi Palindrome, thought there would have been an easier way to what i was doing. I seem to be learning as i go along although the next problem is always a little harder than the last :slight_smile:

thank you for you advice !


Sponsor our Newsletter | Privacy Policy | Terms of Service