Results 1 to 21 of 21

Copying Range Of Numbers Deletes Previously Copied Numbers In Column

Threaded View

  1. #1
    Forum Contributor
    Join Date
    10-29-2019
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    156

    Copying Range Of Numbers Deletes Previously Copied Numbers In Column

    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
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 6
    Last Post: 11-07-2019, 12:36 PM
  2. Finding a range of numbers in a column of numbers
    By charliemacdmv in forum Excel General
    Replies: 1
    Last Post: 10-17-2019, 09:42 PM
  3. Replies: 4
    Last Post: 10-19-2017, 08:12 AM
  4. Replies: 7
    Last Post: 09-08-2017, 03:24 PM
  5. Replies: 1
    Last Post: 04-26-2015, 01:58 PM
  6. [SOLVED] how to copy and insert copied row basied on numbers in column
    By hazemrezo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-10-2013, 08:19 AM
  7. [SOLVED] Numbers copied from a Double Array to a Range.Value2 loses precision
    By TopXorGuy in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-30-2012, 10:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1