如何在VBA中声明数组变量?

时间:2011-04-17 03:48:21

标签: arrays vba

我需要在数组中添加var

Public Sub Testprog()

Dim test As Variant
Dim iCounter As Integer

If test = Empty Then
    iCounter = 0
    test(iCounter) = "test"
Else
    iCounter = UBound(test)
End If
End Sub

test(iCounter) = "test"

处收到错误

请提出一些解决方案

7 个答案:

答案 0 :(得分:31)

通常,您应声明特定类型的变量,而不是Variant。在此示例中,test变量应为String类型。

并且,因为它是一个数组,所以在声明变量时需要明确指出。声明数组变量的方法有两种:

  1. 如果您在编写程序时知道数组的大小(它应包含的元素数),您可以在声明的括号中指定该数字:

    Dim test(1) As String   'declares an array with 2 elements that holds strings
    

    这种类型的数组被称为静态数组,因为它的大小是固定的,或者是静态的。

  2. 如果在编写应用程序时不知道阵列的大小,则可以使用动态阵列。动态数组的大小未在声明(Dim语句)中指定,而是稍后在使用ReDim语句执行程序期间确定。例如:

    Dim test() As String
    Dim arraySize As Integer
    
    ' Code to do other things, like calculate the size required for the array
    ' ...
    arraySize = 5
    
    ReDim test(arraySize)  'size the array to the value of the arraySize variable
    

答案 1 :(得分:12)

除了Cody Gray的回答,还有第三种方式(一切都适用于她):

您还可以使用动态调整大小的动态数组:

Dim test() as String
Dim arraySize as Integer

Do While someCondition
    '...whatever
    arraySize = arraySize + 1
    ReDim Preserve test(arraySize)
    test(arraySize) = newStringValue
Loop

请注意Preserve关键字。没有它,重新定义数组也会初始化所有元素。

答案 2 :(得分:6)

正如其他人所指出的,你的问题是你没有声明一个数组

下面我尝试重新创建您的程序,使其按预期工作。 我试图尽可能多地离开(例如将数组作为变体)

Public Sub Testprog()
    '"test()" is an array, "test" is not
    Dim test() As Variant
    'I am assuming that iCounter is the array size
    Dim iCounter As Integer

    '"On Error Resume Next" just makes us skip over a section that throws the error
    On Error Resume Next

    'if test() has not been assigned a UBound or LBound yet, calling either will throw an error
    '   without an LBound and UBound an array won't hold anything (we will assign them later)

    'Array size can be determined by (UBound(test) - LBound(test)) + 1
    If (UBound(test) - LBound(test)) + 1 > 0 Then
        iCounter = (UBound(test) - LBound(test)) + 1

        'So that we don't run the code that deals with UBound(test) throwing an error
        Exit Sub
    End If

    'All the code below here will run if UBound(test)/LBound(test) threw an error
    iCounter = 0

    'This makes LBound(test) = 0
    '   and UBound(test) = iCounter where iCounter is 0
    '   Which gives us one element at test(0)
    ReDim Preserve test(0 To iCounter)

    test(iCounter) = "test"
End Sub

答案 3 :(得分:5)

除了RolandTumble对Cody Gray的回答之外,这两个很好的答案,这是另一种非常简单灵活的方式,当您在编码时知道所有数组内容时 - 例如你只想构建一个包含1,10,20和50的数组。这也使用变量声明,但不使用ReDim。就像在Roland的回答中一样,数组元素数量的枚举计数不需要具体知道,但可以通过使用uBound获得。

sub Demo_array()
    Dim MyArray as Variant, MyArray2 as Variant, i as Long

    MyArray = Array(1, 10, 20, 50)  'The key - the powerful Array() statement
    MyArray2 = Array("Apple", "Pear", "Orange") 'strings work too

    For i = 0 to UBound(MyArray)
        Debug.Print i, MyArray(i)
    Next i
    For i = 0 to UBound(MyArray2)
        Debug.Print i, MyArray2(i)
    Next i
End Sub

我比其他任何创建数组的方式都更喜欢这个。最棒的是你可以在Array语句中添加或减少数组的成员,而不需要对代码做任何其他事情。要将Egg添加到3元素食物数组中,只需键入

即可

," Egg"

在适当的地方,你已经完成了。你的食物数组现在有4个元素,在昏暗中不需要修改任何东西,完全省略了ReDim。

如果不需要基于0的阵列 - 即使用MyArray(0) - 一种解决方案就是堵塞0或者#34;"第一个元素。

注意,一些编码纯粹主义者可能会认为这很糟糕;一个公平的反对意见是"硬数据"应该在Const语句中,而不是在例程中的代码语句。另一个可能是牛肉,如果你将36个元素粘贴到一个数组中,你应该将const设置为36,而不是忽略它。后一种反对意见是值得商榷的,因为它强制要求用36保持Const而不是依赖于uBound。如果添加第37个元素但将Const保留为36,则可能会出现问题。

答案 4 :(得分:2)

您必须将数组变量声明为数组:

Dim test(10) As Variant

答案 5 :(得分:-1)

David,错误来自Microsoft Office Excel已停止工作。有两个选项可在线检查解决方案并关闭程序和其他选项关闭程序 我确信错误在我的数组中,但我正在阅读所有内容,似乎这是定义数组的方法。

答案 6 :(得分:-3)

Array索引只接受一个long值。

您将iCounter声明为整数。你应该把它声明为长。