繁体   English   中英

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

[英]How do I declare an array variable in 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"出错test(iCounter) = "test"

请提出一些解决方案

通常,您应该声明特定类型的变量,而不是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 

继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关键字。 没有它,重新定义数组也会初始化所有元素。

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

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

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

继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元素食物阵列中,只需键入即可

, “蛋”

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

如果不需要基于0的阵列 - 即,使用MyArray(0) - 一种解决方案就是为第一个元素堵塞0或“”。

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

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

Dim test(10) As Variant

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

Array索引只接受long值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM