+ Reply to Thread
Results 1 to 4 of 4

Timer loop in one workbook disables other workbooks from being edited

Hybrid View

  1. #1
    Registered User
    Join Date
    02-02-2015
    Location
    Portland
    MS-Off Ver
    2013
    Posts
    32

    Timer loop in one workbook disables other workbooks from being edited

    Hello,

    Currently, I have a userform that runs a timer upon clicking 'Start' and stops once 'Stop' is pressed. Once stopped, another userform asking for additional information about the timed session will display and be recorded onto the Excel sheet. My problem is that when the timer is running, users cannot edit other workbooks that they have open. Does anybody have another way of doing this that would allow users to edit other workbooks while this loop is running? Here is the code for it:
    Dim dteStart As Date, dteFinish As Date
        Dim dteStopped As Date, dteElapsed As Date
        Dim boolStopPressed As Boolean, boolResetPressed As Boolean
        
    Private Sub btnReset_Click()
        dteStopped = 0
        dteStart = 0
        dteElapsed = 0
        Label1 = "0:00:00"
        boolResetPressed = True
    End Sub
    
    Private Sub btnStart_Click()
    btnstart.Enabled = False
    btnstop.Enabled = True
    Start_timer:
        dteStart = time
        boolStopPressed = False
        boolResetPressed = False
    Timer_Loop:
        DoEvents
        dteFinish = time
        dteElapsed = dteFinish - dteStart + dteStopped
        If Not boolStopPressed = True Then
            Label1 = WorksheetFunction.Text(dteElapsed, "[h]:mm:ss")
            If boolResetPressed = True Then GoTo Start_timer
            GoTo Timer_Loop
        Else
            Exit Sub
        End If
    
    End Sub
    
    Private Sub btnStop_Click()
        ActiveWorkbook.Save
        ManualReportUF2.Show
        boolStopPressed = True
        dteStopped = dteElapsed
        Call btnReset_Click
        btnstart.Enabled = True
        btnstop.Enabled = False
        Call UserForm_Initialize
        ActiveWorkbook.Save
    
    End Sub
    
    Private Sub UserForm_Initialize()
        Label1 = "0:00:00"
        btnstop.Enabled = False
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Timer loop in one workbook disables other workbooks from being edited

    Try opening the userform as vbmodeless

    Userform1.show vbmodeless
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Timer loop in one workbook disables other workbooks from being edited

    Hi mason0567,

    It seems like you have two problems:
    a. The problem that mehmetcik pointed out. When you open the UserForm as Modal, by definition the rest of the Excel world is locked out in this Instance of Excel until the UserForm Closes. You could access another workbook, if it were opened in a separate instance of Excel.

    b. Your endless loop is a resource hog. If you add a delay in the loop, other workbooks should be able to be accessed. I was only able to access other workbooks using View > Switch Windows. See the changes in red below that added a delay:
    Option Explicit
    
    #If VBA7 And Win64 Then
        ' 64 bit Excel
        'The following line is supposed to be RED in 32 bit Excel
        Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #Else
        ' 32 bit Excel
        Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #End If
    
        Dim dteStart As Date, dteFinish As Date
            Dim dteStopped As Date, dteElapsed As Date
            Dim boolStopPressed As Boolean, boolResetPressed As Boolean
            
        Private Sub btnStart_Click()
        btnStart.Enabled = False
        btnStop.Enabled = True
    Start_timer:
            dteStart = Time
            boolStopPressed = False
            boolResetPressed = False
    Timer_Loop:
            DoEvents
            dteFinish = Time
            dteElapsed = dteFinish - dteStart + dteStopped
            If Not boolStopPressed = True Then
                Label1 = WorksheetFunction.Text(dteElapsed, "[h]:mm:ss")
                Sleep 250   'Delay for 250 milliseconds
                If boolResetPressed = True Then GoTo Start_timer
                GoTo Timer_Loop
            Else
                Exit Sub
            End If
    
        End Sub
    The delay is for 1/4 of a second, even though your granularity is one second. The reason is to allow the application to seem responsive when you click a CommandButton.

    Lewis

  4. #4
    Registered User
    Join Date
    02-02-2015
    Location
    Portland
    MS-Off Ver
    2013
    Posts
    32

    Re: Timer loop in one workbook disables other workbooks from being edited

    Awesome, thanks for the advice. It works perfectly!

+ 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. Loop through workbooks and copy just the first sheet only to master workbook.
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2014, 10:36 AM
  2. Do Until Loop - Transferring data from several workbooks to a Master workbook
    By GalsenPAP in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-10-2013, 01:00 PM
  3. Replies: 1
    Last Post: 07-20-2012, 06:20 PM
  4. Timer Loop
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-20-2010, 01:24 PM
  5. Clear cells when another cell is edited by using a loop
    By Broadway in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-16-2009, 04:21 PM

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