+ Reply to Thread
Results 1 to 7 of 7

Userform Time crossing Midnight not working

  1. #1
    Forum Contributor
    Join Date
    06-29-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2016 (Office 365)
    Posts
    137

    Userform Time crossing Midnight not working

    Hi there,

    I have broken this userform out of a larger program. It is essentially used to set the current time zone and then set a time using the spin buttons. The time on the top is local time and on the bottom is Z time which simply follows along with the local time by adding/subtracting the set time zone. The problem is it goes haywire when you cross midnight. If someone who has experience in adding/subtracting times in vba can have a look over it that would be great.

    Thanks,
    Marcus
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Userform Time crossing Midnight not working

    Marcus

    How does it go haywire?

    Can you give an example?

    By the way, there's nothing to indicate whether the time zone is +/-.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    06-29-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2016 (Office 365)
    Posts
    137

    Re: Userform Time crossing Midnight not working

    Hi Norie,

    Your right I am in the + side of the world so I haven't included the -.

    By haywire I mean if the TZ is +10 and local time is say 14:00 as you use the spin button to move the hours down when the local time gets to 10:00 the Zulu time is 00:00 and then starts counting up so local goes to 09:00 and Zulu goes to 01:00 when is should be 23:00 the previous day.

    Hope that explains it. If you just keep spinning down on the hour button you will see the change.

    Cheers,
    Marcus

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Userform Time crossing Midnight not working

    Marcus

    Try this.
    Please Login or Register  to view this content.

  5. #5
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Userform Time crossing Midnight not working

    I think the whole form could be made simpler by keeping a form-level variable to record the current time. I've created one (called "LocalTime") and then everything else is much simpler. I've also used TimeSerial to make it obvious what amounts of time your adding/subtracting to the local time. Doing it this way means you don't have to keep reading the value from the caption and continually doing a text to time conversion. See attached.

    WBD
    Attached Files Attached Files
    Office 365 on Windows 11, looking for rep!

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Userform Time crossing Midnight not working

    I just thought of more optimizations to prevent having pretty much the same code over and over again. See attached which puts all time adjustment into a single function.

    WBD
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    06-29-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2016 (Office 365)
    Posts
    137

    Re: Userform Time crossing Midnight not working

    Thanks Norrie and WBD I appreciate you taking the time to help.

    WBD thanks heaps for rewriting the whole thing, works brilliantly and saves code over my clunky effort.

    Cheers,
    Marcus

+ 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] 24h time cross midnight; Nested IF not working for an earlier time
    By bren84 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-08-2019, 03:38 PM
  2. [SOLVED] Formula Needed - Hours between times crossing midnight
    By meowmix7903 in forum Excel - New Users/Basics
    Replies: 10
    Last Post: 08-30-2017, 05:52 AM
  3. [SOLVED] Tiimelog - Time Difference Crossing Over or Past Midnight
    By mycon73 in forum Excel General
    Replies: 6
    Last Post: 10-20-2016, 06:41 AM
  4. formula to calculate time difference crossing midnight
    By ditorejax in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-06-2014, 08:27 PM
  5. Time crossing midnight
    By belle08 in forum Excel General
    Replies: 4
    Last Post: 01-27-2014, 03:47 PM
  6. [SOLVED] Time calculation not working past midnight
    By Sophie.Durrant in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-08-2014, 08:18 PM
  7. Replies: 13
    Last Post: 11-04-2013, 02:30 AM

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