ReDim保存在For循环
问题描述:
我不知道我在做什么错误,因为下面的代码能够在第一次迭代中得到ReDim Preserve
但不是第二次迭代。ReDim保存在For循环
Dim inj0() As Variant
Dim i As Integer
Dim c As Integer
Dim Rng As Range
Dim pos As Integer
'Find the last used column in a Row
Dim LastCol As Integer
With ActiveSheet
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
End With
c = 0
For i = 1 To LastCol
pos = InStr(Cells(2, i), "80")
If pos = 1 Then
ReDim Preserve inj0(c, 2)
inj0(0, 1) = "80"
Set Rng = Cells(2, i)
inj0(c, 2) = Rng.Offset(-1, 0).Value
inj0(c, 0) = Rng.Offset(3, 0).Value
c = c + 1
End If
Next
答
尝试更改代码如下:
c = 0
ReDim inj0(2, 0)
inj0(1, 0) = "80"
For i = 1 To LastCol
pos = InStr(Cells(2, i), "80")
If pos = 1 Then
ReDim Preserve inj0(2, c)
Set Rng = Cells(2, i)
inj0(2, c) = Rng.Offset(-1, 0).Value
inj0(0, c) = Rng.Offset(3, 0).Value
c = c + 1
End If
Next
如果你需要被交换的尺寸,终于可以apply WorksheetFunction.Transpose
method。
你从哪里得到错误? –
在'ReDim保留inj0(c,2)'时,它表示下标超出范围 – peetman
您只能'ReDim保留'数组的_last_维度。从联机帮助_如果使用Preserve关键字,则只能调整最后一个数组维度,并且无法全部更改维度数_ –