Trying to build a loop for a repetitive but dynamic data

PaulWilson

New Member
Joined
Aug 3, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. 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.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about:

VBA Code:
    Dim wsCopy          As Workbook
    Dim wsDest          As Workbook
'
    Set wsDest = Workbooks("2").Worksheets("(2)")
'
    For CopyCount = 2 To 0 Step -1
        Set wsCopy = Workbooks("1").Worksheets(Worksheets.Count - CopyCount)
'
        wsCopy.Range("B4:D" & wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row).Copy _
                wsDest.Range("C" & wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row)
'
        wsCopy.Range("I4:K" & wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row).Copy _
                wsDest.Range("C" & wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row)
    Next
 
Upvote 0
Instead of taking data from the last three sheets in the workbook, it is taking data from a random 3 in the middle. I will get back to you after I have done some trial and error.
 
Upvote 0
I found the issue:

The worksheets(worksheets.count) was taking the total number of sheets from the destination workbook instead of the copy workbook. Below are the additions in red:

Workbooks.Open (thisworkbook.Path & "\1")

Dim wsCopy As Workbook
Dim wsDest As Workbook
'
Workbooks.Open (thisworkbook.Path & "\1")
Set wsDest = Workbooks("2").Worksheets("(2)")
'
For CopyCount = 2 To 0 Step -1
workbooks("1").activate
'
Set wsCopy = Workbooks("1").Worksheets(Worksheets.Count - CopyCount)
'
wsCopy.Range("B4:D" & wsCopy.Cells(wsCopy.Rows.Count, "B").End(xlUp).Row).Copy _
wsDest.Range("C" & wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row)
'
wsCopy.Range("I4:K" & wsCopy.Cells(wsCopy.Rows.Count, "I").End(xlUp).Row).Copy _
wsDest.Range("C" & wsDest.Cells(wsDest.Rows.Count, "C").End(xlUp).Offset(1).Row)
Next


Thank you for your help!
 
Upvote 0
I assumed the partial code you posted was working. :rolleyes:

Glad to help.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top