VBA - 如果计时器超过 2 分钟,则退出函数 [英] VBA - Exit Function if a timer passes 2 minutes

查看:74
本文介绍了VBA - 如果计时器超过 2 分钟,则退出函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的 VBA 函数,如果它运行的时间超过 3 分钟,我想停止运行 - 请参阅下面的代码.

I've got a legacy VBA function which I want to stop running if it takes longer than 3 minutes - please see below for code I have so far.

显然,MsgBox 只在函数完成后显示,但我需要它来显示和退出函数.

Obviously, the MsgBox only shows once function has completed but I need it to show and exit a function during.

        Dim startTime As Long
        startTime = GetTickCount

        Do

            If GetTickCount - startTime > 10000 Then
                MsgBox "There seems to be an issue. Please try again!"
                Exit Do
            End If

            doStuff

        Loop

谁能帮帮我?!

非常感谢.

推荐答案

这里还有10秒

Dim startTime As Date

        startTime = Now

        Do

            If DateDiff("s", startTime, Now) > 10 Then
                MsgBox "There seems to be an issue. Please try again!"
                Exit Do
            End If

            dostuff

        Loop

但这仅在 dostuff 的持续时间为 dostuff 时才有效.10 秒.否则,您必须将支票放入 dostuff

But this works only if duration of dostuff is < 10 seconds. Otherwise you must put the check into dostuff

这篇关于VBA - 如果计时器超过 2 分钟,则退出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆