verifying times

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
334
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: 22

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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