大家。这是我编写的一个小型VBA(Excel)函数,其中包含用于调试的MsgBoxes。
我传入数字10和1作为参数,当程序在第一次迭代开始之前到达For循环的顶部时出现溢出错误。
任何想法都表示赞赏。
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Function PerformanceTest(iterations As Integer, interval As Integer) As Double
Dim st, tot, k As Double
Dim n As Integer
tot = 0#
MsgBox "ok"
k = iterations + tot
MsgBox "ookk"
n = 1
MsgBox "assigned"
For n = 1 To iterations
MsgBox n
st = Timer
Application.Calculate
tot = tot + (Timer - st)
Sleep (1000 * interval)
Next n
'MsgBox (tot / k)
PerformancTest = tot / k
End Function
答案 0 :(得分:2)
删除了冗余,基本未更改的函数在我的Excel 2003中运行时没有错误
Function PerformanceTest(iterations As Integer, interval As Integer) As Double
Dim st, tot As Double
Dim n As Integer
For n = 1 To iterations
st = Timer
Application.Calculate
tot = tot + Timer - st
''# Sleep (1000 * interval)
Next n
PerformanceTest = tot / (iterations + tot)
End Function
所以......你看到的错误可能不属于函数本身。
P.S。:专业提示:;-)使用Debug.Print
代替MsgBox
进行调试输出。
答案 1 :(得分:0)
冒着再次看起来像个傻瓜的风险,这里有一些意见。
我会像这样建立我的计时器功能。这对我来说似乎更简单。 (不包括我删除了一些不必要的行 - 我的意思是它在结构上很简单。)
如果它没有溢出,那么这将是一个很好的加分。
Function PerformanceTest(iterations As Integer, interval As Integer) As Double
Dim st, tot, k As Double
Dim n As Integer
PerformanceTest = Timer
k = iterations + tot
n = 1
For n = 1 To iterations
'' insert operation that takes time
Sleep (1000 * interval)
Next n
PerformanceTest = Timer - PerformanceTest
PerformanceTest = PerformanceTest / k
End Function