在VBA中调整Range数组的大小

时间:2014-04-02 20:10:26

标签: vba excel-vba word-vba excel

我声明了一个Range数组

Dim aRange(1 to 5) as Range

我以后无法使用

在我的代码中调整数组大小
ReDim Preserve aRange(1 to a)

我做错了什么或者我无法调整Range数组的大小。

1 个答案:

答案 0 :(得分:2)

变化:

Dim aRange(1 to 5) as Range 'Static dimensioning, cannot be changed later on

由:

Dim aRange() as Range 'Declared as dynamic array, have to be redim before using it

所以:

Dim a as Integer
a = 6    

Dim aRange() as Range 'Declared as dynamic array, have to be redim before using it

ReDim Preserve aRange(1 to 5)
ReDim Preserve aRange(1 to a)

参见 ReDim Statement