公共静态变量在excel vba [英] Public Static variable in excel vba

查看:265
本文介绍了公共静态变量在excel vba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在一个过程中声明一个静态变量,并使用Excel VBA在几个不同的过程中使用此变量?

Is it possible to have a static variable declared in one procedure, and use this variable in several different procedures using Excel VBA?

Public myvar as integer

Sub SetVar()
   static myvar as integer
   myvar=999
end sub

sub Usevar()
    dim newvar as integer
    newvar=myvar*0.5
end sub

我需要myvar被其他程序看到,而不是改变或丢失。如果myvar未声明为静态变量,则上述代码可以工作,但是更多的代码则变量为lost。如果使用静态声明,则使用usevar过程看不到myvar。和公共静态myvar作为整数不被VBA接受。

I need myvar to be seen by other procedures, and not change or get "lost". The code above works if myvar is not declared as a static variable, but more code then the variable is "lost". If the static declaration is used, myvar is not seen by the usevar procedure. And "Public Static myvar as integer" is not accepted by VBA.

感谢您的帮助

宙斯

推荐答案

通过致电 MAIN

Public myvar As Integer

Sub MAIN()
    Call SetVar
    Call UseVar
End Sub

Sub SetVar()
    myvar = 999
End Sub

Sub UseVar()
    Dim newvar As Variant
    newvar = myvar * 0.5
    MsgBox newvar
End Sub

如果您声明项目静态,其值将保留在过程或子目录中。如果您声明项目公开,则其值将被保留,并且其他过程也将可见。

If you declare an item Static , its value will be preserved within the procedure or sub. If you declare the item Public , its value will be preserved and it will be visible to other procedures as well.

这篇关于公共静态变量在excel vba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆