better way to call global config variables from another modules

Viewed 73

I have three modules in my VBA project:

module 1: config_variable

module 2: main_script1

module 3: main_script2

In "config_variable", the codes like this:

Public dir_source1 As String
Public dir_source2 As String
Public dir_source3 As String

Sub global_config()
    dir_source1 = range("A2").value
    dir_source2 = range("A3").value
    dir_source3 = range("A4").value
End Sub

I manage all configurations in this script. And I call this "global_config" Sub for each Sub in module 2 and module 3 to get global variables. Like this:

module 2 - main_script1:

Sub my_sub1()
    ' call global variables
    Call config_variable.global_config
    
    ' main codes for my_sub1
    .....    
End Sub

Sub my_sub2()
    ' call global variables
    Call config_variable.global_config
    
    ' main codes for my_sub2
    .....    
End Sub

module 3 - main_script2:

Sub my_sub3()
    ' call global variables
    Call config_variable.global_config
    
    ' main codes for my_sub3
    .....    
End Sub

The problem is that I need to call the line "Call config_variable.global_config" in the beginning of each Sub.

Is it possible I can just run this line once and every Sub in the same modules can catch the global variables? Like this:

' call global variables on top of each modules
Call config_variable.global_config

Sub my_sub1()
    ' main codes for my_sub1
    .....    
End Sub

Sub my_sub2()
    ' main codes for my_sub2
    .....    
End Sub
3 Answers

Call the setting of the global variables when you launch your workbook or project file:

Call config_variable.global_config

Of course, if the range values may change during a session, you will have to reset the global variabels after such a change.

If settings values are stored in sheet cells, there is no need to move them to global variables at all. You can take those values directly from the cells. For convenience, you can name these cells and refer to them, for short, with [] (syntactic sugar = Evaluate()). If the settings are not called very often, this method may be suitable, requiring no variables to be updated. For example:

Sub run_once() ' or make names manually
    For Each x In Array("dir_source1:A2", "dir_source2:A3", "dir_source3:A4")
        y = Split(x, ":")
        ThisWorkbook.Names.Add y(0), ThisWorkbook.Worksheets("Settings").Range(y(1))
    Next
End Sub

Sub UsageExample()
    Debug.Print "dir_source1 = " & [dir_source1]    ' equivalent for ThisWorkbook.Worksheets("Settings").Range("A2")
End Sub

If "config_variable" is a standard module (as it looks to be), you can simple call the sub as global_config. It is not Private and it will be called. But, I should suggest you to call it only if one of the Public variable is "" (nullString):

If dir_source1 = "" Then global_config

In this way, even if an error occurred and the Public variable lost their value, they will be used only after global_config runs and give values to them.

Related