excel - VBA:如何编辑代码以消除堆栈空间不足的错误?
问题描述
这段代码曾经可以工作,但最近我收到一个错误(堆栈空间不足)。
我认为代码失败是因为我在没有退出/结束的情况下调用了太多次函数。
如果是这种情况,你可以调用一个函数多少次,我能做些什么来解决这个问题?
我不是这段代码的原作者。
我包括了发生错误的子。
Sub CalculatePct(e As Variant)
Dim G As Integer
Dim pct As Double
Dim Owned100Pct As Boolean
If entities(e) < 0 Then
pct = 0
Owned100Pct = True ' Keeps track if the entity exists in the table other than as a parent
For G = 1 To UBound(MainArray, 1)
If MainArray(G, colEntity) = e Then
Owned100Pct = False
If entities(MainArray(G, colParent)) = -1 Then
'If we don't know the parent's ownership percentage, go and calculate it
CalculatePct MainArray(G, colParent)
End If
pct = pct + CDbl(MainArray(G, colPct)) / 100 * entities(MainArray(G, colParent))
End If
Next
If Owned100Pct Then
'Assume 100% owned if we don't know the parentage
'("Outside" entities won't go through here as they are already set to 0%)
entities(e) = 1
Else
'Store the entity's percentage
entities(e) = pct
End If
End If
End Sub
解决方案
您没有说明错误发生在哪一行,也没有说明 MainArray 代表什么,但我猜 MainArray 已经增长到大于可以使用有符号短整数访问的大小。
将迭代变量的声明更改为有符号长整数。这将变量的功能限制从 32,767 次迭代提高到 2,147,483,647 次。
Dim G As Long
推荐阅读
- node.js - 如何使用 node.js 拉取私有 GitHub 存储库(组织)
- mysql - 创建工具时,编辑页面会加载空白 [system-frontier]
- c# - 当有许多网络适配器时,如何找到用于打开 TCP/IP 服务器端口的正确 IP 地址
- youtube - 用于从 youtube 播放列表播放随机视频的魔术链接
- c# - 如何在从 C# 进程开始 CMD 的 CMD 文件中转义 % 字符
- javascript - Wildfly:部署后代码更改不会反映在浏览器中
- c# - 无法将“System.__ComObject”类型的 COM 对象转换为接口类型“Microsoft.VisualStudio.TestTools.UITest.Extension.IUITechnologyElement”
- reactjs - 太多的重新渲染。React 限制渲染次数以防止 React Native 在我的异步无状态组件中的代码中出现无限循环
- c - 为什么 switch 语句中的除法符号 ÷ 给出多字符字符常量警告?据我所知,这是一个角色
- java - 使用 ASM 将 RunTimeVisibleAnnotations 添加到 Java 类文件