+ Reply to Thread
Results 1 to 4 of 4

Copy and Paste rows

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2009
    Location
    Boston
    MS-Off Ver
    Excel 2013
    Posts
    68

    Copy and Paste rows

    Hi,

    I want to copy and paste row in sheet 3 to the row in sheet 1.
    Since the row no is a variable, i m having difficulty in writing the right formula.
    Please help.

    Dim i as Integer
    i=14 ' i keeps on changing ..here sample value is taken as 14
    ' Copy and paste the range A14:AF14
      Sheets("Sheet3").Select
            
       Range("A" & CStr(i) & ":" & "AF" & CStr(i)).Select
       Selection.Copy
       Sheets("Sheet1").Select
       Range("A" & Cstr(i) & ":" & "AF" & Cstr(i)).Select
        ActiveSheet.Paste
    Above code doesn't work
    Last edited by kapilrakh; 11-13-2009 at 04:06 PM.

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Copy and Paste rows

    Hi Kapilrakh

    Try this:

        Dim varRowSource as integer
        Dim varRowTarget as integer
        Dim varRowCount as integer
    
        'count the total number rows on the source sheet
        Sheets("Sheet3").Select
        varRowCount = Application.WorksheetFunction.CountA(Range("A:A"))
    
        'set loop counters to zero
        varRowSource = 2  'Start in row 2 of source sheet
        varRowTarget = 2   'Copy to row 2 of target sheet
            
        'starting with A2 on Sheet3, loop through all positions
        Do Until varRowSource = varRowCount + 1
            Cells(varRowSource, 1).Select
            Selection.Copy
            Sheets("Sheet1").Select
            Cells(varRowTarget, 1).Select
            ActiveSheet.Paste
    
            varRowSource = varRowSource + 1
            varRowTarget = varRowTarget + 1
        Loop

  3. #3
    Registered User
    Join Date
    04-22-2009
    Location
    Boston
    MS-Off Ver
    Excel 2013
    Posts
    68

    Re: Copy and Paste rows

    Hey mojo,

    Thanks for your reply. But your code copies the values cell wise instead of the entire row. It take a lot of time plus I will have to repeat this code for every column. Please put the code to copy and paste entire row. The row no is a variable

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Copy and Paste rows

    To copy a row instead of the cell substitute

    Cells(varRowSource, 1).Select and Cells(varRowTarget, 1).Select

    for

    Row(varRowSource).Select and Row(varRowTarget).Select

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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