Use of Conditional Formatting

Monkeyboy2266

New Member
Joined
Dec 10, 2018
Messages
2
Hi...please be gentle, as its my first post!

I'm attempting to use conditional formatting to return a response of "Mornings" , "Afternoons" or "Nights" from a column which has a time reference.

The boundaries are:-

Mornings are between 06:00 to 13:59
Afternoons are between 14:00 to 21:59
Nights are between 22:00 to 05:59

I've tried a couple of iterations but I've not been successful in my attempts...

Thanks in advance.
Rich
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
try

Code:
=IF(AND(A1 > =TIME(6,0,0),A1 < TIME(14,0,0)),"Mornings",IF(AND(A1 > =TIME(14,0,0),A1 < TIME(22,0,0)),"Afternoons","Nights"))
 
Last edited:
Upvote 0
I think you are referring to creating a formula in a cell, not necessarily a conditional format?
Anyway...
Create a table like this:
00:00 Night
06:00 Morning
14:00 Afternoon
22:00 Night
(lets assume this is in cells A1:B4)
Then write a time in cell D1 and in E1 place this formula:
Code:
=VLOOKUP(D1,$A$1:$B$4,2)
 
Upvote 0
The best way to attack this is not with conditional formatting, but with a macro in the change event

This will change the value of the cell to "Mornings", Afternoons", or "Nights"

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim int_target As Integer
 If Target.Value = "" Then Exit Sub
 If Not IsDate(Target.Value) Then Exit Sub
 
 int_target = Hour(Target.Value)
     If int_target > 5 And int_target < 14 Then Target.Value = "Mornings"
     If int_target > 13 And int_target < 22 Then Target.Value = "Afternoons"
     If int_target < 6 Or int_target < 21 Then Target.Value = "Nights"
End Sub

If you need it to be a formula for another cell then, write it like this...

Code:
Option Explicit

Public Function MakeShift(ByVal myTime As Range)
Dim int_myTime As Integer

    MakeShift = ""
    If myTime.Value = "" Then Exit Function
    If Not IsDate(myTime.Value) Then Exit Function
 
    int_myTime = Hour(myTime.Value)
    If int_myTime > 5 And int_myTime < 14 Then MakeShift = "Mornings"
    If int_myTime > 13 And int_myTime < 22 Then MakeShift = "Mornings"
    If int_myTime < 6 Or int_myTime < 21 Then MakeShift = "Mornings"
     
End Function
 
Upvote 0
Many thanks to all of you that contributed. I hadn't considered using a VLookup, and as I'm familiar with this method, took that route...thanks again to all.
 
Upvote 0

Forum statistics

Threads
1,215,686
Messages
6,126,202
Members
449,298
Latest member
Jest

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