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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
[ 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.