PaulWilson
New Member
- Joined
- Aug 3, 2022
- Messages
- 5
- Office Version
- 2019
- Platform
- Windows
Hi all,
I am trying to find a way to loop 2 sets of instructions to copy and paste data instead of having it written like I currently have below. The first set of instructions takes one range of data from a worksheet, copies, and pastes it into another workbook and the second set of instructions copy and pastes the second range of data into the same destination workbook.
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 2)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("B4:D" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 2)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("I4:K" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 1)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("B4:D" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 1)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("I4:K" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("B4:D" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("I4:K" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
What I am trying to accomplish in this code is to copy a dynamic selection of data from the last three sheets of workbook "1" to a single sheet in workbook "2".
I am unsure as to what to do and have just picked up VBA coding, so any insight/help would be greatly appreciated.
I am trying to find a way to loop 2 sets of instructions to copy and paste data instead of having it written like I currently have below. The first set of instructions takes one range of data from a worksheet, copies, and pastes it into another workbook and the second set of instructions copy and pastes the second range of data into the same destination workbook.
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 2)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("B4:D" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 2)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("I4:K" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 1)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("B4:D" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count - 1)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("I4:K" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("B4:D" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
Set wsCopy = Workbooks("1").worksheets(worksheets.Count)
Set wsDest = Workbooks("2").worksheets("(2)")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row
wsCopy.Range("I4:K" & lCopyLastRow).Copy _
wsDest.Range("C" & lDestLastRow)
What I am trying to accomplish in this code is to copy a dynamic selection of data from the last three sheets of workbook "1" to a single sheet in workbook "2".
I am unsure as to what to do and have just picked up VBA coding, so any insight/help would be greatly appreciated.