[英]Array not populating from For Loop VBA
我正在嘗試創建一個填充for循環的單元格條目數組(例如A6,B6等)。
然而陣列
MyArray
總是空的,我無法弄清楚為什么它沒有被填充在for循環中。 下面是(代碼的相應部分正在做其他事情)我的代碼:
Sub ListSheets()
' Defining all variables (objects) used within the code, establishing their
'classes
Dim i As Integer
Dim array_size As Integer
Dim MyArray() As String
array_size = 26
ReDim MyArray(array_size) As String
For intLoop = 1 To 26
MyArray(intLoop, 1) = Chr$(64 + intLoop) + "6"
Next
Set CopyFrom = MyArray
Sheets("vba_deposit").Range("A1").Resize(CopyFrom.Rows.Count).Value = CopyFrom.Value
End Sub
有任何想法嗎?
提前謝謝了。
試試這個:
Sub ListSheets()
Dim i As Integer
Dim array_size As Integer
Dim MyArray() As String
array_size = 26
ReDim MyArray(1 To array_size, 1 To 1)
For intLoop = 1 To 26
MyArray(intLoop, 1) = Chr$(64 + intLoop) + "6"
Next
Sheets("vba_deposit").Range("A1").Resize(UBound(MyArray)).Value = MyArray
End Sub
你第一次使用MyArray(intLoop, 1)
想法很好 - 因為在這種情況下,不需要使用Transpose
(因為它對數組中元素的數量有限制而不能一直工作): Range(...).Value = MyArray
。 但是我對你的代碼做了一點改動:
ReDim MyArray(1 To array_size, 1 To 1)
Range(...).Value = MyArray
Sub ListSheets()
' Defining all variables (objects) used within the code, establishing their
'classes
Dim i As Integer
Dim array_size As Integer
Dim MyArray() As String
array_size = 26
ReDim MyArray(array_size) As String
For intloop = 1 To 26
MyArray(intloop) = Chr$(64 + intloop) + "6"
Sheets(1).Range("A1").Offset(intloop - 1).Value = MyArray(intloop)
Next
'An array is not an object, you can't use SET with them.
'Your array is 1-dimensional, MyArray(1,1) won't work as that's 2-dimensional, just
'MyArray(1) = "whatever1" MyArray(2) = "whatever2" etc. etc.
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.