VBA code for adding in text based on another cell's time.

bigbeat85

New Member
Joined
May 24, 2017
Messages
23
I have the below table and in column labelled shift I want to be able to populate it with either "Day" or "Night" based on the column labelled Conducted on time only.

I still want to keep the date reference as this is used elsewhere.

For example If the time is between 6am and 6pm I want to label it as "Day". Between 6pm and 6am labelled as "Night".

I think I am down the right path with this if statement but it is not working for me.


Dim LastRow As Long
Dim i As Long
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow


If Range("C" & i).Value < DateValue("DD/MM/YYYY 06:00:00") And Range("C" & i).Value > DateValue("DD/MM/YYYY 18:00:00") Then
Range("D" & i).Value = "EVENING"
End If

Next i

SHIFT ON:Title Page_Conducted onSHIFT
Shift B
23/08/2023 11:58​
Shift C
22/08/2023 05:10​
Shift C
21/08/2023 05:18​
Shift A
19/08/2023 06:09​
Shift B
18/08/2023 18:49​
Shift A
18/08/2023 11:32​
Shift C
17/08/2023 17:09​
17/08/2023 17:07​
Shift D
16/08/2023 05:07​
Shift A
15/08/2023 17:06​
Shift B
15/08/2023 05:33​
Shift B
14/08/2023 05:42​
03/08/2023 11:05​
Shift B
27/07/2023 08:04​
Shift D
20/07/2023 05:05​
Shift D
19/07/2023 13:01​
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Why are you doing this with VBA code? Try the formula in the mini-sheet below.
DayNight.xlsx
ABCD
1SHIFT ON:Title Page_Conducted onSHIFT
2Shift B23/08/2023 11:58DAY
3Shift C22/08/2023 05:10NIGHT
4Shift C21/08/2023 05:18NIGHT
5Shift A19/08/2023 06:09DAY
6Shift B18/08/2023 18:49NIGHT
7Shift A18/08/2023 11:32DAY
8Shift C17/08/2023 17:09DAY
917/08/2023 17:07DAY
10Shift D16/08/2023 05:07NIGHT
11Shift A15/08/2023 17:06DAY
12Shift B15/08/2023 05:33NIGHT
13Shift B14/08/2023 05:42NIGHT
1403/08/2023 11:05DAY
15Shift B27/07/2023 08:04DAY
16Shift D20/07/2023 05:05NIGHT
17Shift D19/07/2023 13:01DAY
Sheet1
Cell Formulas
RangeFormula
D2:D17D2=IF(AND(TIMEVALUE(RIGHT(LEFT(C2,16),5)) > TIME(5,59,59),TIMEVALUE(RIGHT(LEFT(C2,16),5)) < TIME(18,0,1)),"DAY","NIGHT")
 
Upvote 0
I'm using vba as the csv file I use has different departments daily. So I need something to sort the information easily.
 
Upvote 0
I'm using vba as the csv file I use has different departments daily. So I need something to sort the information easily.
try this VBA code
VBA Code:
Option Explicit
Public Sub DayNight()
  Dim LastRow As Long
  Dim i As Long
  Dim tmp
  Dim tv
  LastRow = Range("D" & Rows.Count).End(xlUp).Row
  For i = 2 To LastRow
    tmp = Split(Range("C" & i))
    If UBound(tmp) > 0 Then
      tv = TimeValue(Left(tmp(1), 5))
      Range("D" & i) = IIf(tv > TimeSerial(5, 59, 59) And tv < TimeSerial(18, 0, 1), "DAY", "NIGHT")
    End If
  Next i
End Sub
 
Upvote 0
It hasn't worked for me. The highlighted in bold should that be "If" not "IIF"?
There are no errors that pop up

If I do use If instead of iif it shows as a syntax error?

Dim LastRow As Long
Dim i As Long
Dim tmp
Dim tv
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
tmp = Split(Range("C" & i))
If UBound(tmp) > 0 Then
tv = TimeValue(Left(tmp(1), 5))
Range("D" & i) = IIf(tv > TimeSerial(5, 59, 59) And tv < TimeSerial(18, 0, 1), "DAY", "NIGHT")
End If
Next i
 
Upvote 0
Hi to all. I was working on something like this:
VBA Code:
'[...]
For i = 2 To LastRow
    'If CDate(Mid(Range("C" & i).Value, 11, 6)) < TimeValue("06:00:00") Or CDate(Mid(Range("C" & i).Value, 11, 6)) > TimeValue("17:59:59") Then '(something wrong with the pasted data)
    If CDate(Mid(Range("C" & i).Value, 11, 5)) < TimeValue("06:00:00") Or CDate(Mid(Range("C" & i).Value, 11, 5)) > TimeValue("17:59:59") Then
        Range("D" & i).Value = "NIGHT"
    Else
        Range("D" & i).Value = "DAY"
    End If
Next i
'[...]
 
Upvote 0
Strange, what version of Excel are you using?
Here's the code with IF THEN ELSE statement
VBA Code:
Public Sub DayNight()
  Dim LastRow As Long
  Dim i As Long
  Dim tmp
  Dim tv
  LastRow = Range("D" & Rows.Count).End(xlUp).Row
  For i = 2 To LastRow
    tmp = Split(Range("C" & i)) 'separate time from date
    If UBound(tmp) > 0 Then
      tmp = Split(tmp(1), ":") 'separate hours from minutes
      If UBound(tmp) > 0 Then
        tv = TimeSerial(Val(tmp(0)), Val(tmp(1)), 0)
        If tv > TimeSerial(5, 59, 59) And tv < TimeSerial(18, 0, 0) Then
          Range("D" & i) = "DAY"
        Else
          Range("D" & i) = "NIGHT"
        End If
      End If
    End If
  Next i
End Sub
 
Upvote 0
I'm using Excel 365. I found it odd too. Tried with the new code and still not working. Unless it is a setting in excel I am missing
 
Upvote 0
The invisible character on the end of the values in column C bothered me so here's my go at this
VBA Code:
Sub DayOrNight()
    Dim rng As Range, cel As Range
    Dim tv As Double
    
Set rng = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)
rng.Replace what:=ChrW(8203), Replacement:="", LookAt:=xlPart

For Each cel In rng
    tv = TimeValue(cel)
    If tv >= 0.25 And tv < 0.75 Then
        cel.Offset(, 1) = "Day"
    Else
        cel.Offset(, 1) = "Night"
    End If
Next cel

End Sub
 
Upvote 0
Solution
Perhaps this.

VBA Code:
Public Sub DayNight()
    Dim LastRow As Long
    Dim i As Long
    Dim DT As Date
    Dim TV1 As Date, TV2 As Date
    
    LastRow = Range("C" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If VBA.IsDate(Range("C" & i).Value) Then
            DT = CDate(Range("C" & i).Value)
            DT = DT - Int(DT)
            TV1 = TimeValue("06:00:00")
            TV2 = TimeValue("18:00:00")
            
            If (DT > TV1) And (DT < TV2) Then
                Range("D" & i).Value = "DAY"
            Else
                Range("D" & i).Value = "NIGHT"
            End If
        Else
            Range("D" & i).Value = "not a valid time"
        End If
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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