Need help VBA to figure time from a date/time field ??

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
I am trying to change font color if the date is less than yesterday AND the time is before 16:00.


Hi the fields are in this custom format. m/d/yyyy h:mm

Here's my code so far and it works for the date part, but I can't figure out how to do the cut off time portion.

Code:
LR2 = ELK.Cells(Rows.Count, 1).End(xlUp).Row
xDay = Format(Date, "dddd")
    If xDay = "Monday" Then
        xDate = Date - 3
    Else
        xDate = Date - 1
    End If
  For j = 2 To LR2
    If Cells(j, 14).Value < xDate And Cells(j, 14).Value <> "" Then
        Cells(j, 14).Offset(0, 1).Font.Color = 255
        Cells(j, 14).Offset(0, 2).Font.Color = 255
        End If
    Next j
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
you can shorten your code like this:

Code:
For j = 2 To ELK.Cells(Rows.Count, 1).End(xlUp).Row
    If Cells(j, 14).Value < Application.WorkDay(Date, -1) Then
        If Cells(j, 14).Value - Int(Cells(j, 14).Value) < TimeSerial(16, 0, 0) Then
            If Cells(j, 14).Value <> "" Then
                Cells(j, 14).Offset(0, 1).Font.Color = 255
                Cells(j, 14).Offset(0, 2).Font.Color = 255
            End If
        End If
    End If
Next j
 
Upvote 0

Forum statistics

Threads
1,225,757
Messages
6,186,848
Members
453,379
Latest member
gabriellegonzalez

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