I am trying to build an array containing ranges using a loop so I can paste it all together to earn some time. The loop pastes stock name from column L to another sheet. Then, dynamic values generate in range (CA59:CQ59). I tried transpose function just in case, double loop for manual 2d array,and simpler ranges without luck. My best shot was something like
Sheets("Stocks | Sort").Range("O2:O" & UBound(pool) + 1) = WorksheetFunction.Transpose(pool)
returned one column. Apologize for my lack of knowledge. Any ideas would be highly appreciated. Here is my code :
Sub ForecastAll()
Dim line As Range
Dim pool() As Variant
Dim i As Long
For i = 2 To 133
Sheets("Stocks | Sort").Range("L" & i).Copy (Sheets("Stocks | Synopsis").Range("E3"))
ReDim Preserve pool(i)
pool(i) = Sheets("Stocks | Synopsis").Range("CA59:CQ59").Value
Next i
Sheets("Stocks | Sort").Range("O2:AE133").ClearContents
Sheets("Stocks | Sort").Range("O2:AE133").Value = pool
End Sub
It returns blanks. Thank you for your time in advance.
Made a few changes in your code with explanation. Array populated from Worksheet column or row or table range values is a two dimensional array.
Sub ForecastAll()
Dim pool() As Variant
Dim i As Long
'clearing target range first to fill it with the loop below
Sheets("Stocks | Sort").Range("O2:AE133").ClearContents
For i = 2 To 133
Sheets("Stocks | Sort").Range("L" & i).Copy (Sheets("Stocks | Synopsis").Range("E3"))
ReDim Preserve pool(i)
'pool(i) = Sheets("Stocks | Synopsis").Range("CA59:CQ59").Value
'pool is one dimensional array where in at each iteration you are entering _
'a two dimensional array. Which can be ensured with --
'Debug.Print UBound(pool), Join(Application.Index(pool(i), 1, 0), ",")
'instead of using an array you can directly assign values of one range to another like --
Sheets("Stocks | Sort").Range("O2:AE" & i).Value = Sheets("Stocks | Synopsis").Range("CA59:CQ59").Value
'This method should also work fast as it is not selecting ranges and copying-pasting values _
'If you still want to use array, it should be a 2D array and it can not be populated directly _
'from range and it will need a loop to populate that array
Next i
'Sheets("Stocks | Sort").Range("O2:AE133").ClearContents
'Sheets("Stocks | Sort").Range("O2:AE133").Value = pool
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.