The correct procedure for declaring global variables in VBA?

I have a workbook with several worksheets that exist constantly, which are constantly being cleaned, updated, etc. Since they are mentioned in different routines, I made each corresponding object of the working table of a pseudoglobal variable in the following way, for example, for the main sheet:

    Function MAIN() As Worksheet
        Set MAIN = ThisWorkbook.Sheets("Main")
    End Function

This way, I can refer to each sheet in other routines, for example:

    MAIN.Cells.ClearContents

I also defined some pseudoglobal constants that are located in a fixed place on the "main" sheet in a similar way, for example:

    Function NumLines() As Integer
        NumLines = MAIN.Range("C3").Value
    End Function

, "NumLines" , . , , , , ?

+4
2

Sheet.CodeName Property. CodeName, , .

enter image description here

Range. C3 , . .

enter image description here

, .

Sub Test_Macro()

    Debug.Print MAIN.Name   '>> result: Sheet1
    Debug.Print Range("CellC3").Value   '>> result: 100

End Sub
+4

, , , , ?

VBA, .

  • g_. , VBA . " ?" . , TON - .

    , . VBA , , , . , , .

  • , Option Explicit, , . , , , .

  • , "GlobalVariables" - . all . , , , . " ?" .


- , . VBA ( "" ), , VBA . "Main" .

, MAIN.Cells. KazJaw .

+4

All Articles