【发布时间】:2018-08-14 19:27:02
【问题描述】:
我是编码的初学者,我遇到的问题是程序太大。我读到这可以通过将代码分成更小的模块来解决。但是我想知道当您调用模块内部的变量来修改它时,是否可以考虑模块外部定义的变量的值。另外,修改后可以在模块外和另一个模块中使用新值吗?有人可以解释一下 VBA 在这些情况下是如何工作的吗?非常感谢。
我阅读了您给我的答案,但是虽然我理解这个概念,但没有适合我的例子。我真的很感激一个例子,这样我就可以完全理解如何解决我的问题。我所拥有的基本上是:
sub simulador()
Sheets(2).Activate
With ActiveSheet
a=1
b=1
c=1
for t=2 to 20
procedure using and modifying variables a and c
b=cells(t,15)
for a=2 to 20
code using variable b
next a
d=3
j=9
procedure using the variables a b and j
for ga=2 to 20
code using variable d
next ga
next t
end with
end sub
我需要做的是创建一个 sub1 whit:
procedure using and modifying variables a and c
b=cells(t,15)
一个子2 whit:
for a=2 to 20
code using and modifying variable b
next a
一个子 3 whit:
procedures using the variables a b and j
for ga=2 to 20
code using variable d
next ga
所以最后会是这样的:
sub simulador()
Sheets(2).Activate
With ActiveSheet
a=1
b=1
c=1
for t=2 to 20
Call sub 1
Call sub 2
d=3
j=9
Call sub 3
next t
end with
end sub
在所有情况下,我都想调用传递变量 Byref 的子例程,因此一旦修改它们就保持修改,以便另一个子程序可以将它们与新值一起使用。当这些子程序中的过程被执行时,活动工作表也应该保持活动状态。我无法弄清楚一切是如何运作的。感谢您的帮助。
【问题讨论】:
标签: vba pass-by-reference