宣告陣列Declaring arrays

陣列的宣告相同方式與其他變數相同,方法是使用 DimStaticPrivatePublic 陳述式。Arrays are declared the same way as other variables, by using the Dim, Static, Private, or Public statements. 純量變數 (非陣列的變數) 與陣列變數之間的差異為,您通常必須指定陣列的大小。The difference between scalar variables (those that aren't arrays) and array variables is that you generally must specify the size of the array. 指定大小的陣列是固定大小的陣列。An array whose size is specified is a fixed-size array. 您可以在執行程式時變更其大小的陣列是動態陣列。An array whose size can be changed while a program is running is a dynamic array.

將陣列從 0 或 1 建立索引,取決於 Option Base 陳述式的設定。Whether an array is indexed from 0 or 1 depends on the setting of the Option Base statement. 如果未指定 Option Base 1,則所有的陣列索引會從零開始。If Option Base 1 is not specified, all array indexes begin at zero.

宣告固定陣列Declare a fixed array

在下列程式碼中,固定大小的陣列是宣告為具有 11 列和 11 行的 Integer 陣列:In the following line of code, a fixed-size array is declared as an Integer array having 11 rows and 11 columns:

Dim MyArray(10, 10) As Integer 

第一個引數代表列,第二個引數代表行。The first argument represents the rows; the second argument represents the columns.

如同任何其他變數宣告,除非您為陣列指定資料類型,否則宣告陣列中元素的資料類型為 VariantAs with any other variable declaration, unless you specify a data type for the array, the data type of the elements in a declared array is Variant. 陣列的每個數值 Variant 元素會使用 16 個位元組。Each numeric Variant element of the array uses 16 bytes. 每個字串 Variant 元素會使用 22 個位元組。Each string Variant element uses 22 bytes. 若要撰寫盡可能精簡的程式碼,請將您的陣列明確宣告為 Variant 以外的資料類型。To write code that is as compact as possible, explicitly declare your arrays to be of a data type other than Variant.

下列幾行程式碼會比較數個陣列的大小。The following lines of code compare the size of several arrays.

' Integer array uses 22 bytes (11 elements * 2 bytes). 
ReDim MyIntegerArray(10) As Integer 
 
' Double-precision array uses 88 bytes (11 elements * 8 bytes). 
ReDim MyDoubleArray(10) As Double 
 
' Variant array uses at least 176 bytes (11 elements * 16 bytes). 
ReDim MyVariantArray(10) 
 
' Integer array uses 100 * 100 * 2 bytes (20,000 bytes). 
ReDim MyIntegerArray (99, 99) As Integer 
 
' Double-precision array uses 100 * 100 * 8 bytes (80,000 bytes). 
ReDim MyDoubleArray (99, 99) As Double 
 
' Variant array uses at least 160,000 bytes (100 * 100 * 16 bytes). 
ReDim MyVariantArray(99, 99) 

陣列的大小上限,將根據您的作業系統與可用的記憶體而有所不同。The maximum size of an array varies, based on your operating system and how much memory is available. 使用超出您的系統上可用 RAM 數量的陣列會比較緩慢,因為必須往返磁碟讀取及寫入資料。Using an array that exceeds the amount of RAM available on your system is slower because the data must be read from and written to disk.

宣告動態陣列Declare a dynamic array

透過宣告動態陣列,您便可以在程式碼執行時調整陣列的大小。By declaring a dynamic array, you can size the array while the code is running. 使用 StaticDimPrivatePublic 陳述式來宣告陣列,將括號內保留空白,如下列範例所示。Use a Static, Dim, Private, or Public statement to declare an array, leaving the parentheses empty, as shown in the following example.

Dim sngArray() As Single 

注意

您可以使用 ReDim 陳述式隱含宣告程序內的陣列。You can use the ReDim statement to declare an array implicitly within a procedure. 使用 ReDim 陳述式時請務必注意,不要將陣列的名稱輸入錯誤。Be careful not to misspell the name of the array when you use the ReDim statement. 即使已在模組中包含 Option Explicit 陳述式,系統仍會建立第二個陣列。Even if the Option Explicit statement is included in the module, a second array will be created.

在陣列的 scope 程序內,使用 ReDim 陳述式來變更維度的數量、定義元素數量,以及定義每個維度的上限和下限。In a procedure within the array's scope, use the ReDim statement to change the number of dimensions, to define the number of elements, and to define the upper and lower bounds for each dimension. 您可以使用 ReDim 陳述式,視需要經常變更動態陣列。You can use the ReDim statement to change the dynamic array as often as necessary. 不過,每次您執行此動作時,陣列中現有的值將會遺失。However, each time you do this, the existing values in the array are lost. 使用 ReDim Preserve 來擴大陣列,同時保留陣列中現有的值。Use ReDim Preserve to expand an array while preserving existing values in the array.

例如,下列陳述式會將陣列放大 10 個元素,而不會遺失原始元素目前的值。For example, the following statement enlarges the array by 10 elements without losing the current values of the original elements.

ReDim Preserve varArray(UBound(varArray) + 10) 

注意

使用Preserve 關鍵字搭配陣列時,您只可以變更最後一個維度的上限,但無法變更維度的數量。When you use the Preserve keyword with a dynamic array, you can change only the upper bound of the last dimension, but you can't change the number of dimensions.

另請參閱See also

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.