+ Reply to Thread
Results 1 to 4 of 4

Copy range x times

Hybrid View

  1. #1
    Registered User
    Join Date
    07-27-2018
    Location
    Costa Rica
    MS-Off Ver
    MS Office 2016
    Posts
    17

    Copy range x times

    Hello Team,

    I am working on a little project and trying to copy a range, x amount of times. This is what I have:

    Sub Export()
    
    Application.ScreenUpdating = False
    
    Dim LastColumn As Long
    
    Dim x, y As Integer
    Dim ProRng As Range
    x = 2
    
    LastColumn = Cells(7, Columns.Count).End(xlToLeft).Offset(0, -6).Column 'Calculates number of times I want to copy the range
    
    For Each Cell In Range("G7", Range("G7").End(xlToRight)) 'Range I want to copy is in row 7 and It goes from G7 to the last cell with data at the right in row 7 
        For y = 1 To LastColumn
            Sheets("rawdata").Range("B" & x).Value = Cell.Value 'Need to start copying in cell B2
            x = x + 1
        Next y
    Next Cell
    
        Application.ScreenUpdating = True
        
    End Sub

    So "LastColumn" is the number of times I want to copy the range.

    The range can vary all the time so I just made it automatically with xlToRight. However for example I have the following:

    G7 H7 I7
    E2VA17 E2VA16 E2VA14

    and the code just does this:

    E2VA17
    E2VA17
    E2VA17
    E2VA16
    E2VA16
    E2VA16
    E2VA14
    E2VA14
    E2VA14

    Its kinds good however I need it pasted it like this(lets say that LastColumn = 3, it should copy the range 3 times):

    E2VA17
    E2VA16
    E2VA14
    E2VA17
    E2VA16
    E2VA14
    E2VA17
    E2VA16
    E2VA14


    I really would appreciate any kind of help.

    Regards,
    Luis V.

  2. #2
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Copy range x times

    Try this:

    Sub Export()
    
    Dim c As Range, r As Range, x As Long
    Application.ScreenUpdating = False
    
        Set r = Range("G7", Range("G7").End(xlToRight))
        x = r.Columns.count
        Set c = Sheets("rawdata").Range("B2").Resize(x)
        c = Application.Transpose(r)
        c.Copy c.Resize(x * x)
    
    Application.ScreenUpdating = True
        
    End Sub
    

  3. #3
    Registered User
    Join Date
    07-27-2018
    Location
    Costa Rica
    MS-Off Ver
    MS Office 2016
    Posts
    17

    Re: Copy range x times

    Hi Akuini,

    Thanks for your reply, it worked awesome, however I failed with my calculation because I actually needed to use "LastRow" instead of "LastColumn" as the number of times I want to copy and paste the range.

    
     LastRow = Range("A" & Rows.Count).End(xlUp).Offset(-8, 0).Row
    If I change x in your code it mess it all Can you help me with that?

    I tried but failed:

    Sub Export()
    
    Dim c As Range, r As Range, x As Long
    Dim LastRow As Long
    
    Application.ScreenUpdating = False
    
    LastRow = Range("A" & Rows.Count).End(xlUp).Offset(-8, 0).Row
    
        Set r = Range("G7", Range("G7").End(xlToRight))
        x = LastRow 
        Set c = Sheets("rawdata").Range("B2").Resize(x)
        c = Application.Transpose(r)
        c.Copy c.Resize(x * x)
    
    Application.ScreenUpdating = True
        
    End Sub



    Thanks in advance.

  4. #4
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Copy range x times

    Not sure I understand.
    Let's say the last row with data in col A is row 10, do you mean you want to copy the range 2 times?
    Try this:

    Sub Export2()
    
    Dim c As Range, r As Range, x As Long
    Dim LastRow As Long
    
    Application.ScreenUpdating = False
    
    LastRow = Range("A" & Rows.count).End(xlUp).Offset(-8, 0).Row
    
        Set r = Range("G7", Range("G7").End(xlToRight))
        x = r.Columns.count
        Set c = Sheets("rawdata").Range("B2").Resize(x)
        c = Application.Transpose(r)
        c.Copy c.Resize(x * LastRow)
    
    Application.ScreenUpdating = True
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy and Paste Range Down X times
    By Hyperdude in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2018, 03:38 AM
  2. [SOLVED] Copy range # of times based on cell value.
    By countryfan_nt in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-25-2018, 04:08 AM
  3. copy range from one sheet to another and then copy 5 times skipping ten rows
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-13-2015, 01:35 PM
  4. [SOLVED] copy range value x times in another sheet
    By nikenis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2013, 11:14 PM
  5. [SOLVED] Copy and paste a range x many times
    By acsta14 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-20-2012, 07:15 AM
  6. Copy a Range multiple times
    By alocke in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2011, 12:29 AM
  7. Copy range several times less first row
    By markos97 in forum Excel General
    Replies: 4
    Last Post: 07-28-2010, 08:54 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