Copy multiple rows from one worksheet to another worksheet using macro/vba

dan1974

I've looked around the forum and played with various options but not found a clear match for my problem:

My task is to copy data from a worksheet (called “workorders”) to a second worksheet (called “Assignments”). The data to be copied is from the “workorders” worksheet starting at cell range “E2, P2:S2”; and also copied from each row (same range) until column “P” is empty – (the number of rows to be copied can vary each time we need to run this macro so we can’t select a standard range) . Then pasted into the “Assignments” worksheet, starting at cell “A4”. I’ve used the forum so far to successfully copy a single row of date (from row 2) – I admit that’s the easy part, and I’ve used various versions of code to achieve this. I’ve also tried some code (which I found via watching a youtube clip and modifying http://www.youtube.com/watch?v=PyNWL0DXXtQ )to allow me to run a loop which repeats the copy process for each required row in the “workorders” worksheet and then pastes the data into the “assignments” worksheet- but this is where I am not getting it right, I think I’m along the right lines and think I’m not far off but any help would be very useful.

Code examples below (first 2 only copy first row, 3rd example is where I’ve tried to loop and copy multiple rows:

Sub CopyTest1()
' CopyTest1 Macro
'copy data from workorders sheet
'Worksheets("workorders").Range("E2,P2,Q2,R2,S2").Copy
Worksheets("workorders").Range("E2, P2:S2").Copy
'paste data to assignments sheet
'sheets("assigments dc").Range("A4").Paste
Sheets("Assigments DC").Select
Range("A4").Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub




Sub CopyTest2()
Sheets("workorders").Range("e2,p2,q2,r2,s2").Copy Sheets("assigments dc").Range("a4")
End Sub


Sub CopyTest3()
Dim xrow As Long
'Dim xrow As String
xrow = 2
Worksheets("workorders").Select
Dim lastrow As Long
lastrow = Cells(Rows.Count, 16).End(xlUp).Row
Do Until xrow = lastrow + 1
ActiveSheet.Cells(xrow, 16).Select
If ActiveCell.Text = Not Null Then
'Range("E2,P2,Q2,R2,S2").Copy
'Selection = Range("E2,P2,Q2,R2,S2").Copy
'Cells(xrow, 5).Copy
Cells(xrow, 5).Copy
Sheets("Assigments DC").Select
Range("A4").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Sheets("workorders").Select
End If
xrow = xrow + 1
Loop
End Sub
NullDev

Try this:

Sub LoopCopy()

    Dim shWO As Worksheet, shAss As Worksheet
    Dim WOLastRow As Long, Iter As Long
    Dim RngToCopy As Range, RngToPaste As Range

    With ThisWorkbook
        Set shWO = .Sheets("Workorders") 'Modify as necessary.
        Set shAss = .Sheets("Assignments") 'Modify as necessary.
    End With

    'Get the row index of the last populated row in column P.
    'Change accordingly if you want to use another column as basis.
    'Two versions of getting the last row are provided.
    WOLastRow = shWO.Range("P2").End(xlDown).Row
    'WOLastRow = shWO.Range("P" & Rows.Count).End(xlUp).Row

    For Iter = 2 to WOLastRow
        Set RngToPaste = shAss.Range("A" & (Iter + 2))
        With shWO
            Set RngToCopy = Union(.Range("E" & Iter), .Range("P" & Iter & ":S" & Iter))
            RngToCopy.Copy RngToPaste
        End With
    Next Iter

End Sub

Read the comments first and test.

Let us know if this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy VBA code from one Worksheet to another using VBA code

From Dev

Copy a range from one worksheet to another using cells property

From Dev

excel Copy shapes from one worksheet to another

From Dev

copy a row in one worksheet and paste in another worksheet

From Dev

Copy rows on condition to another Worksheet

From Dev

Copy from all rows one Worksheet and paste in to Another in alternate rows(Excel vba)

From Dev

Copy Range from Worksheet to another Worksheet VBA

From Dev

One worksheet to multiple worksheet

From Dev

Copy Worksheet from one workbook to another. Type Mismatch 13

From Dev

Copy entire worksheet from one instance of Excel to another

From Dev

Excel VBA Copy matching information from one worksheet to another with a loop

From Dev

Copy entire worksheet from one instance of Excel to another

From Dev

Copy data from one worksheet to another based on column

From Dev

copy and paste a cell from one worksheet to another and multiply it by a value

From Dev

how to copy a cell from one worksheet to another, to the correct row

From Dev

Copy range from one worksheet to another based on criterion

From Dev

Copy and Paste Specific Cells from one worksheet to another

From Dev

Copy data from a Pivot table in one worksheet ot another

From Dev

Copy data from one worksheet to another for dynamic lastrow

From Dev

Copy non-merged cells from one worksheet to merged cells in another using a loop

From Dev

How to copy one worksheet to another worksheet on within workbook using aspose.cell

From Dev

How do I copy a worksheet from one workbook into another new workbook with just that worksheet in (with pastevalues)?

From Dev

Copying dynamic rows from one worksheet to another in VBA

From Dev

Copy and Paste a range from one worksheet to multiple worksheets

From Dev

Copy ranges from multiple worksheets into one worksheet, in first empty cell

From Dev

Copying data from one worksheet and paste against relevant rows in another worksheet

From Dev

Excel: Use value in one worksheet to unhide rows in another worksheet

From Dev

Copy worksheet from another workbook including charts

From Dev

copying of specific range of excel cells from one worksheet to another worksheet

Related Related

  1. 1

    Copy VBA code from one Worksheet to another using VBA code

  2. 2

    Copy a range from one worksheet to another using cells property

  3. 3

    excel Copy shapes from one worksheet to another

  4. 4

    copy a row in one worksheet and paste in another worksheet

  5. 5

    Copy rows on condition to another Worksheet

  6. 6

    Copy from all rows one Worksheet and paste in to Another in alternate rows(Excel vba)

  7. 7

    Copy Range from Worksheet to another Worksheet VBA

  8. 8

    One worksheet to multiple worksheet

  9. 9

    Copy Worksheet from one workbook to another. Type Mismatch 13

  10. 10

    Copy entire worksheet from one instance of Excel to another

  11. 11

    Excel VBA Copy matching information from one worksheet to another with a loop

  12. 12

    Copy entire worksheet from one instance of Excel to another

  13. 13

    Copy data from one worksheet to another based on column

  14. 14

    copy and paste a cell from one worksheet to another and multiply it by a value

  15. 15

    how to copy a cell from one worksheet to another, to the correct row

  16. 16

    Copy range from one worksheet to another based on criterion

  17. 17

    Copy and Paste Specific Cells from one worksheet to another

  18. 18

    Copy data from a Pivot table in one worksheet ot another

  19. 19

    Copy data from one worksheet to another for dynamic lastrow

  20. 20

    Copy non-merged cells from one worksheet to merged cells in another using a loop

  21. 21

    How to copy one worksheet to another worksheet on within workbook using aspose.cell

  22. 22

    How do I copy a worksheet from one workbook into another new workbook with just that worksheet in (with pastevalues)?

  23. 23

    Copying dynamic rows from one worksheet to another in VBA

  24. 24

    Copy and Paste a range from one worksheet to multiple worksheets

  25. 25

    Copy ranges from multiple worksheets into one worksheet, in first empty cell

  26. 26

    Copying data from one worksheet and paste against relevant rows in another worksheet

  27. 27

    Excel: Use value in one worksheet to unhide rows in another worksheet

  28. 28

    Copy worksheet from another workbook including charts

  29. 29

    copying of specific range of excel cells from one worksheet to another worksheet

HotTag

Archive