VBA excel time


I'm trying to see if this can be done automatically so i don't have to enter the number manually. Let say A:A i have a drop down list of tire repair, oil change, tire balance, etc.. Is it possible after selecting one of the options the next cell B:B will add the current time. Then next cell C:C will have a time plus a certain amount of minutes added depending what option you pick. so tire repair will be the time plus 45 minutes, or if you select oil change it would be the time plus 30 minutes. Then in column D:D will just have the time it took 30 minutes.

Who is Participating?
[ fanpages ]IT Services ConsultantCommented:

I have created a working example (as attached) that includes the following code (in the Worksheet code module for the [Main] worksheet):

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

' [ http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28673057.html ]
' Q_28673057 "VBA excel time" (fanpages, 13 May 2015)

  Dim objCell                                           As Range
  Application.EnableEvents = False
  For Each objCell In Target
      If objCell.Column = 1 Then
         If IsEmpty(objCell) Then
            Range(objCell.Offset(0, 1), objCell.Offset(0, 2)).ClearContents
            objCell.Offset(0, 1) = Now()
            objCell.Offset(0, 2) = Now() + Application.WorksheetFunction.VLookup(objCell, [VLOOKUP_OPTION_TIME], 2, False)
         End If ' If IsEmpty(objCell) Then
      End If ' If objCell.Column = 1 Then
  Next objCell
  Application.EnableEvents = True
End Sub

Open in new window

Only rows 1 to 20 in the [Main] worksheet have Data Validation applied, but you can easily just copy'n'paste down column [A] as required.

The options & their associated time offsets are stored in the [Options] worksheet.
Roy CoxGroup Finance ManagerCommented:
Here's the basics

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Or Target.Column > 1 Then Exit Sub
Target.Offset(, 1) = Format(Time, "hh:mm")
Target.Offset(, 2) = Format(DateAdd("n", 45, Target.Offset(, 1)), "hh:mm")
End Sub

Open in new window

Add an example workbook and I'll adapt it later for you
HEMIChallengerAuthor Commented:
This works excellent. Thank you very much.
[ fanpages ]IT Services ConsultantCommented:
You are very welcome.

Good luck with the rest of your project.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.