Declaring a global variable from a class module

I have a book with several modules and several submarines. Some variables exist, although they are used constantly in most subsets, such as a given worksheet.

eg,

dim cr as worksheet
set cr=sheets("combined_report")

I have written too many subtitles. Can I write this once in a class module and use "cr" from any sub in any module without rearranging it?

+4
source share
2 answers

Yes, you can!

Typically, you can define a global variable in any module before declaring your subsets / functions with a keyword Global, for example.

Option Explicit

Global cr as Worksheet

Public Sub mySub...

- , , , .. /.

g, . gWSMain, , .

, ! . Sheet1, Sheet2 .., .

, , ThisWorkbook.

+4

Static:

Function CR() As Worksheet
    Static CRSheet As Worksheet
    If CRSheet Is Nothing Then Set CRSheet = Sheets("combined_report")
    Set CR = CRSheet
End Function
+9

Source: https://habr.com/ru/post/1525506/


All Articles