I have to input a LARGE number of "times".
Instead of typing in the Colon, I would like simply to type in e.g. "1147", and then the "Worksheet_Change" routine would change that to "11:47", as a time, that I can then work with - in particular, to subtract from another to give the duration.
No, Marc,, I didn't forget to DO it - I couldn't find out HOW to do it.
Thank you for pointing me to "forum rules"; though a Link would have been more useful.
I'll have a rummidge . . .
Right before change cell, turn off Events then turn on, to avoid Change-event active again
your original code should be:
PHP Code:
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim myRow As Integer, myCol As Integer Dim Hours, Minutes, myValue, myAnswer myRow = Target.Row myCol = Target.Column myValue = Cells(myRow, myCol) If myCol = 3 Or myCol = 8 Or myCol = 13 Then Hours = Val(Left(Cells(myRow, myCol), 2)) Minutes = Val(Right(Cells(myRow, myCol), 2)) myAnswer = TimeSerial(Hours, Minutes, 0) Application.EnableEvents = False Cells(myRow, myCol) = myAnswer Application.EnableEvents = True End If End Sub
And, shorter version
PHP Code:
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("C:C,H:H,M:M")) Is Nothing Then Exit Sub ' do nothing if change in other range Dim Hours, Minutes, myAnswer Hours = Val(Left(Target, 2)) Minutes = Val(Right(Target, 2)) myAnswer = TimeSerial(Hours, Minutes, 0) Application.EnableEvents = False ' turn off change event Target.Value = myAnswer Application.EnableEvents = True ' turn on change event End Sub
My suggestion wasn't a one-liner, it was a THREE-liner!
If you don't include the disabling/enabling of events, then 01:29 is exactly what you'll get when you enter 1145 - when the routine enters the "calculated" time value, that is taken as a Worksheet_Change event which triggers the routine again ... and again ... and again ...
The attached workbook (with event disabling/enabling included) delivers 11:45 when 1145 is entered by the User.
Bookmarks