+ Reply to Thread
Results 1 to 3 of 3

Copy and paste into last row of other sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    15

    Copy and paste into last row of other sheet

    Hi all. I have 2 sheets; Master and Result. I want to run a code that if row n, col C of Master satisfies some condition, I will copy the whole row and paste into the last row of the Result sheet. Both sheet have header, same order header. So when it paste into Result sheet, it will start with row 2, then row 3, etc...which way is the best way to paste into the last row + 1 (because the last row already contains data) of sheet Result?
    Thank guys

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Copy and paste into last row of other sheet

    Hi
    Post sample data...
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Copy and paste into last row of other sheet

    It's really hard to fully understand without a sample workbook. With that said, the following is untested, but should at the very least get you going in the right direction.

    Please make a copy of your workbook and test the code on your copy.

    Option Explicit
    
    Sub SendToMaster()
    
    Dim ws1           As Worksheet
    Dim ws2           As Worksheet
    Dim rngCell       As Range
    Dim rngsource     As Range
    Dim rngDest       As Range
    Dim lngLastCol    As Long
    Dim lngThisRow    As Long
    
    Set ws1 = ActiveWorkbook.Sheets("Master")
    Set ws2 = ActiveWorkbook.Sheets("Result")
    
       With Application
          .ScreenUpdating = False
          .EnableEvents = False
       End With
    
          With ws1
             Set rngsource = .Range("C2", .Range("C2").End(xlDown))
             lngLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
          End With
       
          With ws2
             Set rngDest = .Range("A2", .Range("A50000").End(xlUp)).Offset(1, 0)
          End With
       
          For Each rngCell In rngsource
             'You will need to change the logic within the If statement below as it was
             'not provided in your original post
             If rngCell.Value > 0 Then
                lngThisRow = rngCell.Row
                Set rngToCopy = Range(Cells(lngThisRow, 1), Cells(lngThisRow, lngLastCol))
                rngToCopy.Copy
                rngDest.PasteSpecial xlPasteValues
             End If
          Next
    
       With Application
          .ScreenUpdating = True
          .EnableEvents = True
       End With
    
    End Sub
    Last edited by AlvaroSiza; 04-18-2012 at 12:15 PM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

+ 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