我试图创建一个基于可变输入量存储大量输入的数组。试图使用我的代码数组:
Dim cellValue, x, dupval As Long
dupval = dupbox.Value
Dim anArray()
With ActiveSheet
.Cells(14, 4) = dupval
End With
x = 0
For i = 1 To dupval
MsgBox (i)
cellValue = InputBox("What is the first row of the duplicate?")
anArray(i) = Cells(cellValue, 3)
Next i
For i = anArray(1) To anArray(dupval)
x = x + anArray(i).Value
Next i
在我的第一个for循环中anArray(i)
返回Subsript out of range
。我的工作表中的单元格值为0。有什么想法吗?
答案 0 :(得分:2)
Dim anArray(), _
aValue As Long
aValue = 100 'or anything else
ReDim anArray(aValue)
For i = 1 To aValue
anArray(i) = cellValue
'Or
anArray(i) = cells(i,1)
Next i
anArray = Range("C12:E22").Value
对于最后一部分,anArray(1,1)
将是C12值。
您可以使用LBound(anArray, Dimension)
或UBound(anArray, Dimension)
因此,您可以使用For i = LBound(anArray, 1) or UBound(anArray, 1)