需要解释一下,为什么这个功能只有有时才能正常工作

我正在完成一本书中的练习。我得到了任务 - 编写自己的除法函数,而不使用运算符 (/) 和 (*)。我的实现如下。

Public counter As Long
Public Function myDivide() As Long
    Dim a As Long,b As Long
    counter = 1
    a = Cells(1,activeCell.Column).Value
    b = Cells(2,activeCell.Column).Value
    divide a,b
    myDivide = counter
End Function
Private Function divide(ByRef a As Long,ByRef b As Long) As Long
    If a > b Then
        If a - b <> 0 Then
            counter = counter + 1
            divide a - b,b
        End If
    End If
End Function

执行结果,当将“=myDivide()”函数放置到屏幕截图第3行的每个单元格时

需要解释一下,为什么这个功能只有有时才能正常工作

我的问题:为什么“F3”单元格返回错误#VALUE? 据我所知,Long 可以包含到 2147483647 的数字,并且它大于 10 000 000。我所有的变量都是 Long。可能是,一些堆栈限制? 谢谢!

yanchengwanghao 回答:需要解释一下,为什么这个功能只有有时才能正常工作

恐怕是Excel版本和电脑资源有关...

通过从单元格调用 UDF 函数来测试它,不可能捕获错误。它只是返回“#VALUE!”。 Excel 以这种方式处理此类函数,以避免每次触发 Calculation 事件时出错。

Sub 调用它可以捕获错误(“堆栈空间不足”)并返回所用环境的递归最大数。

本文链接:https://www.f2er.com/2468.html

大家都在问