我想用Array填充一系列单元格,但我无法做到。
1)
Dim Principal() As String
Dim txt As String
For i = LBound(Principal) To UBound(Principal)
txt = txt & Principal(i) & vbCrLf
Next i
MsgBox txt
2)
Dim Principal() As String
Dim txt As String
For i = LBound(Principal) To UBound(Principal)
Worksheets("Sheet1").Cells(i, 3) = Principal(i)
Next i
我可以看到第一个例子中的输出,即MsgBox,但是当我尝试存储到不同的单元格时,我收到错误。提前谢谢。
答案 0 :(得分:3)
Alex和Sorceri已经指出默认情况下,数组从索引0开始。
除此之外,您不需要循环将数组传递到范围。
' 1D Array
With Worksheets("Sheet1")
.Range("C1:C" & Ubound(Principal) + 1) = Application.Transpose(Principal)
End With
记下+ 1
。如果Option Base = 1
则您必须将其删除
另请注意,如果您将1D数组传递到范围,则需要Transpose
数组。