gpt4 book ai didi

VBA 全局变量在子程序开始时重置为 0

转载 作者:行者123 更新时间:2023-12-02 21:39:15 29 4
gpt4 key购买 nike

我正在编写一个使用全局变量的项目。全局变量在模块的开头声明,所有代码都在同一模块内。全局变量(应该)在每个子例程之间传递,并且由于工作表的性质,它们的值根据需要在不同的子例程或函数中声明。

我的问题是,每次调用子例程时,我的全局变量都会重置为 0,这显然违背了全局变量的目的。

有什么明显的原因可能导致这种情况吗?我的代码相当大(1200 多行),在这里发布是不切实际的。

电子表格中的形状已被删除并重新绘制。这是否会重新编译工作表并导致所有全局变量重置?

谢谢

编辑:变量声明

Public Type Blockwall
Asd As Single 'max area of reinforcement allowed
Ast As Single 'Area of reinforcement in design tension zone
Bar As Integer 'bar size
Capacity As Single 'Calculated capacity of wall usign Ast
cover As Single 'cover to reinforcement
d As Single 'Depth to centre of tension steel
Depth As Single 'Thickness of wall/footing
DesignMoment As Single 'Design Moment in base of wall
DL As Load 'Dead Load force
LL As Load 'Live Load Force
fc As Single 'Compressive strength of concrete/grout
fm As Single 'compressive strength of masonry
fsy As Single 'Design stress of steel
Height As Single 'Total height of wall/Length of Footing (sorry it is confusing)
Height190 As Single 'Height of 190 blockwork
Height290 As Single 'Height of 290 blockwork
Moment25 As Single 'moment 25% from the top
Moment50 As Single 'Moment 50% from the top
Moment75 As Single 'Moment 75% from the top
Phi As Single 'Capacity reduction factor
Spacing As Single 'Bar Spacing
X As Single 'Distance of resultant vertical force (Rotation Check)
End Type

Dim Wall As Blockwall
Dim Footing As Blockwall

以及为变量 Footing.Depth 赋值的子例程片段(请注意,这只是为其赋值的一个位置):

Public Sub DrawWall(fLength As Single, fHeight As Single, kLength As Single, kHeight As Single, _
wHeight As Single, distToKey As Single, distToWall As Single, fBeta As Single, fPhi As Single, _
fDensity As Single, nBeta As Single, nPhi As Single, nDensity As Single, LL As Single, Height290 As Single)

'***---ASSIGN VALUES TO GLOBAL VARIABLES---***
Footing.Depth = fHeight
Footing.Height = fLength

子DrawWall被其他子调用来绘制所需的形状。当调用 DrawWall 时,它似乎不会重置值,只有当我单击调用子例程的按钮时(或者我从代码编辑窗口启动子例程)。

最佳答案

事实证明,一旦包含这些命令的子例程完成,OLEObjects(用作输入框)的创建和删除就会导致全局变量重置。 (非常感谢@Rory 找到了那个。)不幸的是,监 window 口不会更新该值,直到您启动下一个子例程(我不知道为什么)。我可能会考虑使用类而不是类型,以便无论如何存储变量。

感谢大家的帮助!

关于VBA 全局变量在子程序开始时重置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34585223/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com