簡體   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