verifying times

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet where I input the start and end time of an activity.

These times are inputted in the "Custom 0\:00" format to allow easy entry. For instance, when I type 0605 then 06:05 automatically appears.

Now I need to ensure that the end time is later than the start time. I want VBA to do this.

The only way I think is possible is to first convert 0605 to a numerical value = 6 hours and 5 minutes = 365 minutes.

Do the same with the end time, and compare both values.

But how to write this in VBA ?
 

Attachments

  • starttime.png
    starttime.png
    11.4 KB · Views: 9

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This can be done by Data Validation.
Select C2:C4
Data --> Data valdation
Allow: Custom (select)
Formula: Enter formula C2>B2
Ok.
 
Upvote 0
You wont need to convert.

VBA Code:
Sub ValidEntry()
    Dim lr As Long: lr = Range("A" & Rows.Count).End(xlUp).Row
    Dim i As Long
    
    For i = 2 To lr
        If Range("C" & i).Value <= Range("B" & i).Value Then
            Range("D" & i).Value = "End time must be later than start time"
        End If
    Next i
End Sub
 
Upvote 0
Worksheet event can be used. Range C2:C4 can be changed as required.
Code
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range
Application.EnableEvents = False
Set R = Range("C2:C4")
If Not Intersect(Target, R) Is Nothing Then
If Target <= Target.Offset(0, -1) Then
Target = ""
MsgBox ("Time should be more than " & Format(Target.Offset(0, -1), "hh:mm"))
End If
End If
Application.EnableEvents = True
End Sub

How to use workheet event the code
Right click on Sheet tab --> view code
Visual Basic (VB) window opens.
Paste the code
Close the VB window.
Save the file as .xlsm
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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