ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Timeouts on VBA code (https://www.excelbanter.com/excel-programming/410073-timeouts-vba-code.html)

Nils Magnus

Timeouts on VBA code
 
Hello,

Is it possible to make VBA code time out after a while if it still
hasn't finished? I'm working on a simulation, and depending on the
parameters, it can sometimes keep on processing without ever reaching an
answer. I cannot know in advance what parameters give answers, and what
parameters don't.

What I would like is that if an answer hasn't been found in 10 seconds,
it should write "Inconclusive" or something similar in the result cell.
How can I do this, if it's possible?

Thanks :-)


Regards,
Nils Magnus

Tom Hutchins

Timeouts on VBA code
 
One way...

Declare a Boolean variable. Set it to FALSE before you start the processing
loop. Also, just before you start the loop, set up an OnTime event which will
change the variable to TRUE. Test the variable within your processing loop;
if it is TRUE, stop processing and return the "inconclusive" result. For
Example:

Public StopNow As Boolean

Sub AAAAA()
'Set StopNow to FALSE
StopNow = False
'Set StopNow to TRUE in 10 seconds.
Application.OnTime Now() + TimeValue("0:00:10"), "Inconclusive"
'Start loop
Do While ... 'not valid; example only
If StopNow = True Then
MsgBox "Inconclusive"
Exit Sub
End If
'Do processing

'Include a DoEvents
DoEvents
Loop
End Sub

Private Sub Inconclusive()
StopNow = True
End Sub

Hope this helps,

Hutch

"Nils Magnus" wrote:

Hello,

Is it possible to make VBA code time out after a while if it still
hasn't finished? I'm working on a simulation, and depending on the
parameters, it can sometimes keep on processing without ever reaching an
answer. I cannot know in advance what parameters give answers, and what
parameters don't.

What I would like is that if an answer hasn't been found in 10 seconds,
it should write "Inconclusive" or something similar in the result cell.
How can I do this, if it's possible?

Thanks :-)


Regards,
Nils Magnus



All times are GMT +1. The time now is 06:09 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com