How do I declare a global variable in VBA?

前端 未结 8 1088

I wrote the following code:

Function find_results_idle()

    Public iRaw As Integer
    Public iColumn As Integer
    iRaw = 1
    iColumn = 1
相关标签:
8条回答
  • 2020-11-22 09:24

    The question is really about scope, as the other guy put it.

    In short, consider this "module":

    Public Var1 As variant     'Var1 can be used in all
                               'modules, class modules and userforms of 
                               'thisworkbook and will preserve any values
                               'assigned to it until either the workbook
                               'is closed or the project is reset.
    
    Dim Var2 As Variant        'Var2 and Var3 can be used anywhere on the
    Private Var3 As Variant    ''current module and will preserve any values
                               ''they're assigned until either the workbook
                               ''is closed or the project is reset.
    
    Sub MySub()                'Var4 can only be used within the procedure MySub
        Dim Var4 as Variant    ''and will only store values until the procedure 
    End Sub                    ''ends.
    
    Sub MyOtherSub()           'You can even declare another Var4 within a
        Dim Var4 as Variant    ''different procedure without generating an
    End Sub                    ''error (only possible confusion). 
    

    You can check out this MSDN reference for more on variable declaration and this other Stack Overflow Question for more on how variables go out of scope.

    Two other quick things:

    1. Be organized when using workbook level variables, so your code doesn't get confusing. Prefer Functions (with proper data types) or passing arguments ByRef.
    2. If you want a variable to preserve its value between calls, you can use the Static statement.
    0 讨论(0)
  • 2020-11-22 09:28

    A good way to create Public/Global variables is to treat the Form like a class object and declare properties and use Public Property Get [variable] to access property/method. Also you might need to reference or pass a Reference to the instantiated Form module. You will get errors if you call methods to forms/reports that are closed.
    Example: pass Me.Form.Module.Parent into sub/function not inside form.

    Option Compare Database 
    Option Explicit
    ''***********************************''
    ' Name: Date: Created Date Author: Name 
    ' Current Version: 1.0
    ' Called by: 
    ''***********************************''
    ' Notes: Explain Who what when why... 
    ' This code Example requires properties to be filled in 
    ''***********************************''
    ' Global Variables
    Public GlobalData As Variant
    ''***********************************''
    ' Private Variables
    Private ObjectReference As Object
    Private ExampleVariable As Variant
    Private ExampleData As Variant
    ''***********************************''
    ' Public properties
    Public Property Get ObjectVariable() As Object
       Set ObjectVariable = ObjectReference
    End Property 
    Public Property Get Variable1() As Variant 
      'Recommend using variants to avoid data errors
      Variable1 = ExampleVariable
    End property
    ''***********************************''
    ' Public Functions that return values
    Public Function DataReturn (Input As Variant) As Variant
       DataReturn = ExampleData + Input
    End Function 
    ''***********************************''
    ' Public Sub Routines
    Public Sub GlobalMethod() 
       'call local Functions/Subs outside of form
       Me.Form.Refresh
    End Sub
    ''***********************************''
    ' Private Functions/Subs used not visible outside 
    ''***********************************''
    End Code
    

    So in the other module you would be able to access:

    Public Sub Method1(objForm as Object)
       'read/write data value
       objForm.GlobalData
       'Get object reference (need to add Public Property Set to change reference object)
       objForm.ObjectVariable
       'read only (needs Public property Let to change value)
       objForm.Variable1
       'Gets result of function with input
       objForm.DataReturn([Input])
       'runs sub/function from outside of normal scope
       objForm.GlobalMethod
    End Sub
    

    If you use Late Binding like I do always check for Null values and objects that are Nothing before attempting to do any processing.

    0 讨论(0)
提交回复
热议问题