+ Reply to Thread
Results 1 to 11 of 11

Worksheet_Change and Time

  1. #1
    Registered User
    Join Date
    11-18-2011
    Location
    Dorset
    MS-Off Ver
    Excel 365
    Posts
    37

    Worksheet_Change and Time

    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.

    But I just CANNOT get it to work !

    Please Login or Register  to view this content.
    The format of the relevant columns is Time as hh:mm
    Last edited by RobinClay; 01-31-2023 at 03:25 PM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Worksheet_Change and Time


    As you forgot again to use the code tags I will help once you edit your post and do the necessary according to forum rules …

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Worksheet_Change and Time

    Hi there,

    See if the following code does what you need:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Worksheet_Change and Time


    As a reminder this beginner level subject just needs few codelines (around ten) without any conversion but just a text association …

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Worksheet_Change and Time

    Just a comment!

    Of course, if you're happy with something which simply "works", you could just make the highlighted amendments to your code

    Please Login or Register  to view this content.
    Assuming no input errors are made by the User, this will "work", but good design it ain't!


    Greg M

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Worksheet_Change and Time


    According to Excel basics that just needs around ten codelines obviously even if « errors are made by the user » …

  7. #7
    Registered User
    Join Date
    11-18-2011
    Location
    Dorset
    MS-Off Ver
    Excel 365
    Posts
    37

    Re: Worksheet_Change and Time

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

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Worksheet_Change and Time


    As you have been warned several times for this and as you already had the explanation how to in your previous threads …

  9. #9
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,459

    Re: Worksheet_Change and Time

    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 IntegermyCol As Integer
    Dim Hours
    MinutesmyValuemyAnswer
    myRow 
    Target.Row
    myCol 
    Target.Column
    myValue 
    Cells(myRowmyCol)
    If 
    myCol Or myCol Or myCol 13 Then
        Hours 
    Val(Left(Cells(myRowmyCol), 2))
        
    Minutes Val(Right(Cells(myRowmyCol), 2))
        
    myAnswer TimeSerial(HoursMinutes0)
        
    Application.EnableEvents False
            Cells
    (myRowmyCol) = 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(TargetRange("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 
    Quang PT

  10. #10
    Registered User
    Join Date
    11-18-2011
    Location
    Dorset
    MS-Off Ver
    Excel 365
    Posts
    37

    Re: Worksheet_Change and Time

    Thank you, Greg, for your "one liner" but . . .

    When I type in 1145, I expect 11:45 as a time, but I get 01:29 ! I'm confused.
    Last edited by RobinClay; 01-31-2023 at 03:38 PM.

  11. #11
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Worksheet_Change and Time

    Hi again,

    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.

    Hope this helps.

    Regards,

    Greg M
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Worksheet_Change
    By hk57 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-24-2020, 01:18 PM
  2. Run time error 13 when deleting more than one cell with Worksheet_Change event macro
    By som3on3_10 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-22-2010, 12:19 PM
  3. VBA worksheet_change
    By jayers in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2008, 08:18 AM
  4. Block Worksheet_Change part of the time?
    By whisperinghill in forum Excel General
    Replies: 2
    Last Post: 01-17-2007, 07:11 PM
  5. worksheet_change help please
    By AmyTaylor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2006, 02:44 PM
  6. Worksheet_change
    By wAyne in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2006, 02:55 PM
  7. [SOLVED] Worksheet_Change: Date Time Input [mmddyy hhmm] to [mm/dd/yy hh:mm]
    By Brian Handly in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-31-2005, 12:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1