Adding a name to specific time slots within an Excel spreadsheet

DivineCupcakes777

New Member
Joined
Jul 5, 2016
Messages
4
I work in Advertising Sales and when I send clients their daily logs in excel, some need to have day parts added to the corresponding time slots. An example of this is:

Date Scheduled Time Aired Program Name
7/4/2016 6:48:08 AM Early Morning
7/4/2016 6:50:08 AM Early Morning
7/4/2016 6:53:24 AM Early Morning
7/4/2016 6:55:14 AM Early Morning
7/4/2016 6:58:38 AM Early Morning
7/4/2016 9:01:34 AM Daytime
7/4/2016 9:17:25 AM Daytime
7/4/2016 9:23:20 AM Daytime

Everything is generated from our reporting except the Program Name, which is the corresponding day part based on when the actual program aired. I would need a macro or a way to have the day part automatically filled out based on the time that the program aired. Here are the day part parameters:

Early Morning: 6am-8:59am
Daytime: 9am-3:59pm (Monday - Friday)
Fringe: 4pm-7:59pm (Monday-Friday)
Prime: 8pm-11:59pm
Late Prime: 12am-1:59am
Overnight: 2am-5:59am
Weekend: 9am-7:59pm (Saturday and Sunday)

Would you have any idea how to do this in Excel? Although I use it extensively, I am a novice when it comes to Excel formulas and macros. Any help would be appreciated! :)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I would attempt a select case statement to identify the time and adjust for weekend.

Code:
Function TimeSlot$(dSched As Date)
Dim bWeekDay As Boolean
Dim dTime As Date


If Weekday(dSched, vbMonday) < 6 Then bWeekDay = True
dTime = Format(dSched, "hh:mm:ss")


Select Case dTime
    Case 0 To 0.082639
        TimeSlot = "Late Prime"
    Case 0.083333 To 0.2493
        TimeSlot = "Overnight"
    Case 0.25 To 0.3743
        TimeSlot = "Early Morning"
    Case 0.833333 To 0.999
        TimeSlot = "Prime"
    Case 0.375 To 0.666
        If bWeekDay Then
            TimeSlot = "Daytime"
        Else
            TimeSlot = "Weekend"
        End If
    Case 0.6666 To 0.8327
        If bWeekDay Then
            TimeSlot = "Fringe"
        Else
            TimeSlot = "Weekend"
        End If
End Select
End Function


Excel 2010
ABC
1Date SchedueledTime AiredProgram Name
27/4/16 6:48:08 AMEarly Morning
37/4/16 6:50:08 AMEarly Morning
47/4/16 6:53:24 AMEarly Morning
57/4/16 6:55:14 AMEarly Morning
67/4/16 6:58:38 AMEarly Morning
77/4/16 9:01:34 AMDaytime
87/4/16 9:17:25 AMDaytime
97/4/16 9:23:20 AMDaytime
Sheet1
Cell Formulas
RangeFormula
B2=TimeSlot(A2)
 
Upvote 0
I was looking into the times a bit closer... Post #2 isn't as specific for time cutoffs so there are a few seconds between the time change that wouldn't fall within one of the cases.

This version uses the excel TimeValue function to convert a string version of the time into a number.

Code:
Function TimeSlot$(dSched As Date)
Dim bWeekDay As Boolean
Dim dTime As Date


If Weekday(dSched, vbMonday) < 6 Then bWeekDay = True
dTime = Format(dSched, "hh:mm:ss")


Select Case dTime
    Case 0 To TimeValue("1:59:59 AM")
        TimeSlot = "Late Prime"
    Case TimeValue("2:00 AM") To TimeValue("5:59:59 AM")
        TimeSlot = "Overnight"
    Case 0.25 To TimeValue("8:59:59 AM")
        TimeSlot = "Early Morning"
    Case TimeValue("20:00:00") To TimeValue("23:59:59")
        TimeSlot = "Prime"
    Case TimeValue("9:00:00") To TimeValue("15:59:59")
        If bWeekDay Then
            TimeSlot = "Daytime"
        Else
            TimeSlot = "Weekend"
        End If
    Case TimeValue("16:00") To TimeValue("19:59:59")
        If bWeekDay Then
            TimeSlot = "Fringe"
        Else
            TimeSlot = "Weekend"
        End If
End Select
End Function
 
Upvote 0
I've attempted to enter this as a TimeValue Function but I must be putting in the wrong area because it keeps showing the formula in the excel spreadsheet. Would you be able to tell me the correct place to enter the formula? Sorry...I am a novice. :confused:
 
Upvote 0
The code portion is for VBA. To add the code to your worksheet, follow these steps:
  • Press [Alt+F11] to open Visual Basic for Applications.
  • In the menu bar, select Insert > Module
  • Paste code in this new Module

When done, close the VB windows and you should be back on your worksheet.

Since we are adding code to your workbook, you would need to save your workbook as a macro-enabled document (XLSM extension)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,368
Messages
6,124,523
Members
449,169
Latest member
mm424

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