HarlanJ
Industrial
- Jan 24, 2008
- 7
I've written a macro that parses text files into memory, modifies the values, and then outputs the new values to a spreadsheet. I have it written to cycle through a set number of lines in a text file before outputting the data, pausing for a moment (ex. 5 seconds), and repeating the process. However, the end user will need to pause the execution indefinately from time to time and this is achieved by a button and textbox at the moment.
I would like to forego the textbox and have the code pause until a second button is clicked, allowing the macro to resume parsing the text file and updating the data.
Here is the current setup:
"Pause" is a boolean public variable updated when a command button on the active worksheet is clicked (changes to "true"). A textbox is displayed, notifying the user that the macro has been paused, and execution stops until the user closes it.
"UserToldProgramToWait" is the subroutine I'm playing with that would ideally hold execution indefinately like the text box.
After the user clicks the Resume button on the worksheet, "UserWantsToResume" is then True and the code continues as normal. A side effect of this method is that Excel will fail to respond after 10-15 seconds. Otherwise, this works fine.
I'm guessing Windows XP determines that a program is failing to respond if it continues to execute the same code without change for more than 15 seconds. I have no problems breaking the operation with the Escape key after Excel 'Stops Responding'.
Any ideas?
I would like to forego the textbox and have the code pause until a second button is clicked, allowing the macro to resume parsing the text file and updating the data.
Here is the current setup:
Code:
If Pause = True Then 'if the user wants to pause the output a message box notifies them and asks to continue
Sheets("Start Page").PauseLabel.Caption = "Paused"
' While timer < Wait + 2.05 'time in seconds to wait while the "Paused" Label is updated
' DoEvents 'do nothing
' Wend
Call UserToldProgramToWait
If MsgBox("Paused. Continue?", vbYesNo, "Dryer Data Extraction Tool") = vbNo Then
Close #1
Pause = False 'The user decided to quit, so the program no longer needs to be paused
Sheets("Start Page").PauseLabel.Caption = ""
Exit Sub
End If
Pause = False 'the user decided to continue, so the program is no longer paused
Sheets("Start Page").PauseLabel.Caption = ""
End If
"Pause" is a boolean public variable updated when a command button on the active worksheet is clicked (changes to "true"). A textbox is displayed, notifying the user that the macro has been paused, and execution stops until the user closes it.
"UserToldProgramToWait" is the subroutine I'm playing with that would ideally hold execution indefinately like the text box.
Code:
Public Sub UserToldProgramToWait()
Dim Wait As Double
Wait = timer
Do Until UserWantsToResume = True 'Until the user clicks the resume button the program will run this loop and pause for 5 seconds before checking again.
While timer < Wait + 10 'wait 10 seconds before checking again
DoEvents 'Do nothing
Wend
Loop
UserWantsToResume = False 'Right after the user decides to resume code execution, This variable is reset until we need to wait again.
End Sub
After the user clicks the Resume button on the worksheet, "UserWantsToResume" is then True and the code continues as normal. A side effect of this method is that Excel will fail to respond after 10-15 seconds. Otherwise, this works fine.
I'm guessing Windows XP determines that a program is failing to respond if it continues to execute the same code without change for more than 15 seconds. I have no problems breaking the operation with the Escape key after Excel 'Stops Responding'.
Any ideas?