Excel VBA错误:数组长度固定或临时被锁定(错误 10)

Excel VBA错误,数组长度固定或临时被锁定(错误 10):并非所有数组都可重设大小。即使数组声明成动态,或者数组是在 Variant 变量中,也会被临时锁定。此错误有以下的原因和解决方法: Excel VBA error, the array length is fixed or temporarily locked (error 10): Not all arrays can be resized. Even if the statement into a dynamic array, or array is Variant variables, will be temporarily locked. This error has the following causes and solutions: 1、使用 ReDim 来改变固定大小数组的元素数。例如,在下列的代码中,在 NextOne 过程中 SomeArr 接收了固定大小的数组 FixedArr ,然后试图调整 SomeArr 的大小: 1, the use of fixed-size array ReDim to change the number of elements. For example, in the following code, in NextOne process SomeArr receiving a fixed-size array FixedArr, and then trying to adjust SomeArr size:Sub FirstOne Dim FixedArr(25) As Integer ‘ 创建一个固定大小的数组并 NextOne FixedArr() ‘ 将之传给其它过程。End SubSub NextOne(SomeArr() As Integer) ReDim SomeArr(35) ‘ 发生错误 10。 . . .End Sub 将数组用 ReDim(如果是在过程内声明数组)声明为动态的而非固定大小,或在声明时不指定元素数(如果是在模块级别中声明数组 )。 The array with ReDim (if the array is declared within a procedure) is declared as dynamic rather than fixed size, or in the declaration does not specify the number of elements (if it is declared at the module level array). 2、对模块层次动态数组,要重新确定大小,而某一个元素已经作为参数传给了过程。例如,在下列的代码中,ModArray 是模块层次动态数组,却将其第 56 个元素以按引用传给 Test过程: 2, the module-level dynamic arrays, to re-determine the size, but one element of the process has been passed as a parameter. For example, in the following code, ModArray is the module-level dynamic array, has launched its first 56 elements in order to pass the Test procedure by reference:Dim ModArray () As Integer ‘ 创建一个模块层次动态数组。 . . .Sub AliasError() ReDim ModArray (1 To 73) As Integer Test ModArray (45) ‘ 传模块层次动态数组的元素 ‘ 给 Test 过程。End SubSub Test(SomeInt As Integer) ReDim ModArray (1 To 40) As Integer ‘ 错误在这里发生。End Sub 此例并不需要传递模块层次动态数组元素,因为在模块中所有过程都可看到它。然而,如果传递元素,在过程内引用参数,数组会被锁定以避免内存的配置释放,因此,当过程返回时,会导致不能预测的情况出现。 This case does not need to pass the module-level dynamic array element, because all of the procedures in the module can see it. However, if the transfer element, a reference parameter within the procedure, the array will be locked to prevent the release of the memory configuration, therefore, when the procedure returns, it will lead to unpredictable situations. 3、赋一个值给包含数组的 Variant 变量,但 Variant 当前被锁定。例如,如果代码使用了 For Each…Next 循环,对包含数组的 variant 执行操作的话,在进入循环后数组将会被锁定,在循环退出后释放: 3, assigning a value to a Variant variable containing an array, but the Variant is currently locked. For example, if your code uses a For Each … Next loop, a variant containing an array implementation of the operation, then enter the cycle in the array will be locked out of the loop after the release:SomeArray = Array(9,8,7,6,5,4,3,2,1)For Each X In SomeArray SomeArray = 301 ‘ 因为数组锁定导致错误。Next X 使用 For…Next 代替 For Each…Next 循环来执行迭代。当数组是 For Each…Next 循环的对象时,可以读入数组,但不能写入。 Instead of using the For … Next For Each … Next loop to perform iteration. When the array is a For Each … Next loop object, you can read into the array, but can not write.
躲在某一地点,想念一个站在来路,

Excel VBA错误:数组长度固定或临时被锁定(错误 10)

相关文章:

你感兴趣的文章:

标签云: