Public Static variable in excel vba

前端 未结 3 839
小鲜肉
小鲜肉 2021-02-05 19:33

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

i.e.

Public m         


        
3条回答
  •  不思量自难忘°
    2021-02-05 19:43

    Try this by calling 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.

提交回复
热议问题