vba - 获取 "Out of Stack Space error",不确定为什么/在我的代码中存在递归

标签 vba excel

我有一个Excel/Vba程序,它通过Excel网格上的命令按钮“输入”一个“点”,输入“点”的列中的所有行然后用于对“适合性”进行评分该点在网格中的位置(出于我现在不会讨论的原因)。

我正在尝试编写另一个命令按钮代码,其中包含:

如果我输入一个“点”&(由于其他条件等),该列的[第 79 行] =“p”,则复制该列的[第 75 行]中的值。 (我们称之为“上方”

然后沿着 [第 79 行] 向后搜索(到 C 列),查找其中存在“r”的所有单元格/列。

对于其中存在“r”的每一列,将“Aboverow's”值粘贴到该列的 [第 80 行]。

我已经为此编写了代码,但我不断收到“堆栈空间不足错误”,我尝试对其进行调试,但不确定哪里出错了。代码如下。任何帮助将非常感激。

Private Sub commandButton12_Click()

    For Checkcol = 3 To 801

        Dim Isnote As Variant

        GoSub Isnote

Isnote:
        If Cells(78, Checkcol) <> "o" Then
            GoSub Isap
        Else
        End If

        Dim Isap As Variant        
Isap:
        If Cells(80, Checkcol) = "p" Or Cells(80, Checkcol) = "pf" Or Cells(80, Checkcol) = "ps" Then
            GoSub Myprocess
        ElseIf Cells(80, Checkcol) = "r" Or Cells(80, Checkcol) = "rf" Or Cells(80, Checkcol) = "rs" Then
        End If

        Dim Myprocess As Variant

        Dim Reg As Variant    
        Reg = "r"        

        Dim Rainj As Variant

        If ActiveCell.Column = Range("J79") Then
            Rainj = Range("J79") & ("C79")
        End If

        Dim Aboverow As Variant

Aboverow:
        If ActiveCell.Column = Range("J79") Then
            Range("J75").Select
            Selection.Copy
        End If

        Dim Belowrow As Variant    
Belowrow:
        If Range("I79") = Reg Then
            Range("I80").Select
        End If

Myprocess:
        GoSub Aboverow
        For Each Reg In Rainj
            GoSub Belowrow
            Selection.Paste
        Next Reg
    Next

End Sub

最佳答案

正如评论中提到的,这就是为什么您应该始终像躲避瘟疫一样避免 GoToGoSub。堆栈溢出的直接原因是您正在调用 GoSub 并且没有任何 Return 语句。这意味着在这部分代码中(我注意到的第一个无限递归,但可能不是唯一的)......

Aboverow:
        If ActiveCell.Column = Range("J79") Then
            Range("J75").Select
            Selection.Copy
        End If

        Dim Belowrow As Variant

Belowrow:
        If Range("I79") = Reg Then
            Range("I80").Select
        End If

Myprocess:
        GoSub Aboverow
        For Each Reg In Rainj
            GoSub Belowrow
            Selection.Paste
        Next Reg

...您的执行从 GoSub Belowrow 行跳转,然后直接返回到 Myprocess: 标签下方的代码,该标签跳转到上面 Belowrow: 标签,它又回到 Belowrow: 标签下面的代码,这......你明白了。

<小时/>

虽然您可以尝试使用 Return 退出“Subs”,但由于这个确切原因,这种编码风格在 20 世纪 70 年代的某个时候就消失了。如果需要子程序,就应该制作子程序。请注意,这只是一个示例 - 流程控制非常复杂,我不知道代码应该做什么:

Sub AboveRow()
    If ActiveCell.Column = Range("J79") Then
        Range("J75").Select
        Selection.Copy
    End If
End Sub

Sub BelowRow()
    If Range("I79") = Reg Then
        Range("I80").Select
    End If
End Sub

Sub Myprocess()
    AboveRow
    For Each Reg In Rainj
        BelowRow
        Selection.Paste
    Next Reg
End Sub

那么上面引用的代码将变成:

    AboveRow
    BelowRow
    Myprocess

请注意,依赖 Select 和跟踪事件单元格还存在许多其他问题,但这至少应该让您更接近可以开始对此进行一些更有意义的调试的程度。

关于vba - 获取 "Out of Stack Space error",不确定为什么/在我的代码中存在递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42255169/

相关文章:

excel - 使用 openpyxl 使用电子表格的行创建对象列表

excel - 检测是否安装了 Excel 2003 的最佳方法是什么?

javascript - 如何在 Javascript 中创建从右到左方向的 Excel 工作表

performance - 在 Excel VBA 中迭代类

vba - 创建引用父类的类 - 最佳实践

vba - 如果列值存储在变量中,则更新单元格中的公式

excel - 为什么使用 MSXML v3.0 解析 XML 文档可以,而 MSXML v6.0 却不行

asp.net - 如何自动化 HTML 可搜索查询数据并将其导出到 Excel

excel - 使用复选框通过 Onaction 传递参数

excel - 将 32 位的代码转换为 64 位