Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialising global variables in VBA

Tags:

excel

vba

In Excel 2003, how can I declare global variables and initialise them only once, ie when the workbook is opened?

I have some parameters that are used by a few macros: paths to input files, basically. At the moment, my code looks like this:

global path1, path2 as string

sub initPaths
    path1 = 'path\to\file1'
    path2 = 'path\to\file2'
end sub

Then, whenever I need to use file1 or file2 in a subroutine or function, I insert a call to initPaths. But this seems rather inelegant; I'd like to be able to set the paths only once rather than repeatedly.

like image 528
Hong Ooi Avatar asked Feb 13 '11 23:02

Hong Ooi


People also ask

Are there global variables in VBA?

VBA global variables are variables declared before any macro in the module starts. When the variables are declared using either “Public” or “Global,” it becomes a global variable. Sub Procedure Variables Cannot Use Anywhere.

How do you initialize a variable in VBA?

In VBA it is not possible to declare and initialise a variable in one line. You must declare your variable on one line and then assign the value on a new line. Explicitly assigning a value to a local variable after it has been declared is recommended.

How do you set a public variable in VBA?

Go to Insert tab, click on Module to add a new module under VBE. Step 2: Add Option Explicit within the module. Step 3: Define a new variable named var1 and var2 both as a type integer. But this time using Public statement instead of Dim (which we used previously).

How do I create a global constant in Excel VBA?

You use the Const statement to declare a constant and set its value. After a constant is declared, it cannot be modified or assigned a new value. You can declare a constant within a procedure or at the top of a module, in the Declarations section. Module-level constants are private by default.


1 Answers

From your example, it looks like what you want are constants, not global variables.

Public Const PATH1 = "path\to\file1"
Public Const PATH2 = "path\to\file2"

If you really do need to use code to determine the values, but only want to initialize them once, you can use lazy-initialization...

Private mstrPath1 As String
Private mstrPath2 As String

Public Function Path1() As String
    if mstrPath1 = vbNullString Then
        ' Initialize mstrPath1 value here.
    End If
    Path1 = mstrPath1
End Function

Public Function Path2() As String
    if mstrPath2 = vbNullString Then
        ' Initialize mstrPath2 value here.
    End If
    Path2 = mstrPath2
End Function

The nice thing here is that if your code ever gets reset, the values simply get re-initialized again next time you access them via their respective functions.

Note that global variables are to be avoided as much as possible, and you should always prefer private global variables over public ones where possible. Use globals and public globals where necessary, but only where necessary.

like image 123
Steve Jorgensen Avatar answered Sep 19 '22 06:09

Steve Jorgensen