类模块的全局变量声明

时间:2022-02-07 22:37:12

I have a workbook with multiple modules and multiple subs. There are some variables though that are usesd constantly in most subs such as given worksheest.

我有一个包含多个模块和多个子的工作簿。虽然有一些变量在大多数潜艇中不断使用,例如给定的工作台。

eg

例如

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

I have this written in way too many subs. Can I write this once in say a class module and use "cr" from any sub in any module without having to reassign it?

我写的太多了。我可以在一个类模块中写一次,并在任何模块的任何子模块中使用“cr”而无需重新分配吗?

2 个解决方案

#1


4  

Yes, you can!

是的你可以!

Generally, you can define a global variable in any module before the declaration of your subs/functions using the Global keyword, e.g.

通常,您可以在使用Global关键字声明子/函数之前在任何模块中定义全局变量,例如

Option Explicit

Global cr as Worksheet

Public Sub mySub...

The global variable will keep it's scope - but of course you have to initialize is first, i.e. assign a value/object to it.

全局变量将保持其范围 - 但当然,您必须首先初始化,即为其分配值/对象。

It is best practice to prefix global variable with a g, e.g. gWSMain, so you'll always know you're dealing with a global variable.

最佳做法是使用g作为全局变量的前缀,例如: gWSMain,所以你总是知道你正在处理一个全局变量。

In case you only want a global variable for one or more specific worksheets in your workbook, you don't need a global variable at all! Instead, you can access them directly with their code names. Those are usually Sheet1, Sheet2, etc. - but you can change the name in the properties window.

如果您只需要工作簿中的一个或多个特定工作表的全局变量,则根本不需要全局变量!相反,您可以使用其代码名称直接访问它们。这些通常是Sheet1,Sheet2等 - 但您可以在属性窗口中更改名称。

These worksheets are available globally in your application, the same way as ThisWorkbook is.

这些工作表在您的应用程序中全局可用,与ThisWorkbook相同。

#2


9  

You can do this with a function in a standard module and cache the reference using the Static keyword:

您可以使用标准模块中的函数执行此操作,并使用Static关键字缓存引用:

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

#1


4  

Yes, you can!

是的你可以!

Generally, you can define a global variable in any module before the declaration of your subs/functions using the Global keyword, e.g.

通常,您可以在使用Global关键字声明子/函数之前在任何模块中定义全局变量,例如

Option Explicit

Global cr as Worksheet

Public Sub mySub...

The global variable will keep it's scope - but of course you have to initialize is first, i.e. assign a value/object to it.

全局变量将保持其范围 - 但当然,您必须首先初始化,即为其分配值/对象。

It is best practice to prefix global variable with a g, e.g. gWSMain, so you'll always know you're dealing with a global variable.

最佳做法是使用g作为全局变量的前缀,例如: gWSMain,所以你总是知道你正在处理一个全局变量。

In case you only want a global variable for one or more specific worksheets in your workbook, you don't need a global variable at all! Instead, you can access them directly with their code names. Those are usually Sheet1, Sheet2, etc. - but you can change the name in the properties window.

如果您只需要工作簿中的一个或多个特定工作表的全局变量,则根本不需要全局变量!相反,您可以使用其代码名称直接访问它们。这些通常是Sheet1,Sheet2等 - 但您可以在属性窗口中更改名称。

These worksheets are available globally in your application, the same way as ThisWorkbook is.

这些工作表在您的应用程序中全局可用,与ThisWorkbook相同。

#2


9  

You can do this with a function in a standard module and cache the reference using the Static keyword:

您可以使用标准模块中的函数执行此操作,并使用Static关键字缓存引用:

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

相关文章