I posted a similar post before, but the script had a lot of irrelevant code. Ive simplified the code for readability. Also if its still not easy to understand, instead of not replying let me know & i'll post a simpler version.
Basically I need to copy a range of numbers from an array to different cell positions, but it deletes previous copied numbers in that column. ie., copy 1,2,3,4 stored in an array to Range("F37:F52").
Im guessing I need to resize the array to match the number of values inside it. Ie., 4. But I dont know how to resize it, as I'm new to arrays.
The line causing the problem is on line 100 : Range(CLabelStartPositionx & ":" & CTotalEndPosition) = Array2
Thanks.,
Sub Arrayx()
Dim Array1 As Variant
Dim Array2() As Variant
Workbooks("testx14New4Main.xlsm").Worksheets(1).ListBox1.Clear
'Detect Last Row in Column A'
With ActiveSheet
LastRow = Workbooks("testx14New4Main.xlsm").Worksheets(1).Cells(.Rows.Count, "A").End(xlUp).Row
End With
Dim CellLoop(1 To 3) As Integer
CellLoop(3) = 120 ' 2Min
CellLoop(2) = 60 '1Min
CellLoop(1) = 12 '10Seconds
'Contains Start Position of Range To Copy To ie C37, C32, C29 '
Dim LabelPosition(1 To 3) As Integer
LabelPosition(3) = 29 '2Min
LabelPosition(2) = 32 '1Min
LabelPosition(1) = 37 '10Seconds
'Contains End Position of Range To Copy To ie Add 12 to C37 = C53, Add 4 to C32 = C36'
Dim LoopCount(1 To 3) As Integer
LoopCount(3) = 2 '2Min
LoopCount(2) = 4 '1Min
LoopCount(1) = 12 ' 10Seconds
IncrementOuterLoop = 1
IncrementInnerLoop = 1
'OuterLoop
'Cycles Through Cell Starting Points in LabelPosition(1) ie., F37, F32, F29'
For j = 1 To 3
'Copies Cells From A1 to C8660 to Array '
Array1 = Range("a1:b8660").Value
ReDim Array2(1 To UBound(Array1), 1 To UBound(Array1, 2))
'Sets Starting Position of Cell To Copy To ie LabelPosition(1) = F37, LabelPosition(2) = F32'
LabelPositionx = LabelPosition(IncrementOuterLoop)
'A1. Create Table'
'A1.Create Labels'
'Cycles Through Cell Starting Points in LabelPosition(1) ie., F37, F32, F29'
'Creates Specific Range To Copy Numbers 1,2,3,4 in Array2 To - ie., Creates Range("F37:F52"), Range("F32:F36")
'Using CLabelStartPositionx & CTotalEndPosition as Range(CLabelStartPositionx & ":" & CTotalEndPosition)'
LabelStartPositionx = LabelPosition(IncrementOuterLoop)
LabelEndPosition = CellLoop(IncrementOuterLoop)
TotalEndPosition = LabelStartPositionx + LabelEndPosition
CLabelStartPositionx = "f" & LabelStartPositionx
CTotalEndPosition = "f" & TotalEndPosition
NumberOfCellsToCopyFromArray=LoopCount(IncrementOuterLoop)
'Inner Loop
'For Loop Determines Number of Values To Copy From Array, ie 1 to 4 '
For x = 1 To 4
'Copy Numbers 1,2,3,4 in Array1 To Array2'
Array2(IncrementInnerLoop, 1) = Array1(IncrementInnerLoop, 1)
IncrementInnerLoop = IncrementInnerLoop + 1
Next x
msgbox "array2 " & Array2(IncrementInnerLoop, 1) & " CLabelStartPositionx " & CLabelStartPositionx & " CTotalEndPosition " & CTotalEndPosition & " NumberOfCellsToCopyFromArray " & NumberOfCellsToCopyFromArray
'Copy Numbers 1,2,3,4 in Array2 to Range ie., Range("F37:F52"), Range("F32:F36")
Range(CLabelStartPositionx & ":" & CTotalEndPosition) = Array2
IncrementOuterLoop = IncrementOuterLoop + 1
IncrementInnerLoop = 1
Erase Array2
Erase Array1
Next j
End Sub
Bookmarks