VBA:数组和全局变量声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7097684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:47:23  来源:igfitidea点击:

VBA: Arrays and Global Variable Declarations

arraysexcel-vbaglobal-variablesscopevba

提问by Etienne No?l

I need to declare an array in VBA that will be used by every function. However, I cannot declare it as a global as I would do in C++.

我需要在 VBA 中声明一个将由每个函数使用的数组。但是,我不能像在 C++ 中那样将其声明为全局变量。

My code is as follows:

我的代码如下:

Option Explicit
 Dim test(0 to 10) as String

 test(0) = "avds"
 test(1) = "fdsafs"
 ....

The following conceptualizes what I am trying to do.

以下概念化了我正在尝试做的事情。

 public function store() as boolean
  Worksheets("test").cells(1,1) = test(0)
 End Function

How can I achieve this functionality?

我怎样才能实现这个功能?

回答by Justin Self

For global declaration, change Dim to Public like so:

对于全局声明,将 Dim 更改为 Public,如下所示:

Public test(0 to 10) as String

You can call this like (assuming it is in Module1, else change Module1 to whatever you've named it):

您可以这样称呼它(假设它在 Module1 中,否则将 Module1 更改为您命名的任何内容):

Module1.test(0) = "something"

Or simply:

或者干脆:

test(0) = "something"

回答by ja72

Why wouldn't you create everything in a class? That's the reason why classes where invented after all.

为什么不在类中创建所有内容?这就是为什么要发明类的原因。

Consider the Class1definition

考虑Class1定义

Option Explicit

Private m_data() As String

Private Sub Class_Initialize()
    ReDim m_data(0 To 10)
End Sub
Private Sub Class_Terminate()
    Erase m_data
End Sub

Public Property Get Count() As Integer
    Count = UBound(m_data) - LBound(m_data) + 1
End Property

Public Property Get Data(index As Integer) As String
    Data = m_data(index)
End Property

Public Property Let Data(index As Integer, value As String)
    m_data(index) = value
End Property

Public Function Store(rng As Range) As Boolean
    Store = (rng.value = m_data(0))
End Function

You can add all the functions you want that can access your array just like Store(). with the test code in a worksheet of

您可以添加可以访问数组的所有所需函数,就像Store(). 使用工作表中的测试代码

Public Sub Test()
    Dim c As New Class1

    c.Data(0) = "January"

    Debug.Print c.Store(Cells(1, 1))
End Sub

You can also cache the location of the cell where it is referencing, or used an assumed named argument and only supply a reference to the worksheet once after class initialization.

您还可以缓存它所引用的单元格的位置,或者使用假定的命名参数,并且在类初始化后只提供一次对工作表的引用。

回答by JMax

You can use the Publickeyword to declare a variable that you need to access in any module.

您可以使用Public关键字来声明您需要在任何模块中访问的变量。

Remember that in vba you cannot declare variables or code outside of procedures.

请记住,在 vba 中,您不能在过程之外声明变量或代码。

See herefor more information

请参阅此处了解更多信息

回答by Michael Harc

I have a recommendation that is a bit lighter than a class (although class is a great recommendation)

我有一个比 class 轻一点的推荐(虽然 class 是一个很好的推荐)

Option 1

选项1

Define your desired constant array as a delimited string constant:

将所需的常量数组定义为分隔字符串常量:

Public Const cstrTest = "String 1;String 2; String 3; String 4; String 5; String 6"

Next, whenever you need it just use Split to create an array with minimal code:

接下来,只要您需要,只需使用 Split 以最少的代码创建一个数组:

Dim arrStrings
arrStrings = Split (cstrTest, ";")

Option 2

选项 2

You might replace (or combine with Option 1) a simple public function

您可以替换(或与选项 1 结合)一个简单的公共函数

Public Function constStringArray() As String()

    constStringArray = Split (cstrTest, ";")

End Function

So then, in use...

那么,在使用...

Dim arrStrings

'Option 1 example
arrStrings = Split (cstrTest, ";")

'Option 2 example
arrStrings = constStringArray()

回答by Andreas Dietrich

one can do it (with global initialization) via Static Propertyquite straight-forward without creating a class or string parsing - as described in detail and with examples here

人们可以通过Static Property非常直接的方式(使用全局初始化)来完成它,而无需创建类或字符串解析 - 如详细描述和示例here