VBA拆分代码和传递参数

时间:2018-08-14 19:27:03

标签: vba pass-by-reference

我是一个编码的初学者,我遇到的问题是过程太大。我读到可以通过将代码分成较小的模块来解决此问题。但是我想知道,当您在模块内部调用变量进行修改时,是否可以考虑模块外部定义的变量的值。另外,在修改之后,您可以在模块外部和另一个模块中使用新值吗?有人可以解释一下在这种情况下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白色:

procedure using and modifying variables a and c
b=cells(t,15)

2级以下的白色:

for a=2 to 20
code using and modifying variable b
next a

3级以下:

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的子例程,因此一旦对其进行修改,就保持修改状态,以便另一个子例程可以将它们与新值一起使用。当执行这些子例程中的过程时,活动工作表也应保持活动状态。我不知道一切如何正常工作。感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

我没有时间花时间解释它的工作原理,因为这些是编程的基础知识,所以....在Google上搜索:

  

vb函数传递参数差byref和byval

相关问题