vba 如何在vba中的另一个函数中使用一个函数的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12778722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:01:39  来源:igfitidea点击:

How to use variable of one function in another function in vba?

excel-vbavbaexcel

提问by M_S_SAJJAN

I am facing one small problem please help me.

我面临一个小问题,请帮助我。

sub vari()
dim b as integer
dim c as integer
b=6
c=8
end sub

now i want to use b and c in another function .

现在我想在另一个函数中使用 b 和 c。

sub calculate()
dim a as integer
a = c+b
end sub

I am getting error here

我在这里遇到错误

回答by Asad Saeeduddin

You want to use the proper scopefor your variables. If you want for b and c to be accessible to both vari() and calculate() you need to declare them globally, like so:

您想为变量使用适当的范围。如果您希望 vari() 和 calculate() 都可以访问 b 和 c,则需要全局声明它们,如下所示:

Public b As Integer
Public c As Integer
sub vari()
....