0

I am creating a macro that will copy all data from one workbook/multiple sheets into another workbook/multiple sheets. The first spreadsheet has 7 worksheets named as Sun-Sat. The second worksheet has 10 worksheets 3 worksheets are irrelevant and the other 7 worksheets are named Sunday-Saturday.

I tested each for loop separately and they work as needed. When trying to combine them the inner for statement repeats and cycles through all dates before backing out. I have tried incorporating a exit for to jump out of the inner for but when going back to the inner for it does not increment +1 to go to the next date. Is there a simple way to add +1 from the outer for statement?

enter code here
Dim wsShortDays, wsFullDays As Variant
Dim wsShortDaysCrnt, wsFullDaysCrnt As Long
Dim SD, FD As Long

wsShortDays = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
wsFullDays = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

For FD = LBound(wsFullDays) To UBound(wsFullDays)
    With wbk1.Worksheets(wsFullDays(FD))

        For SD = LBound(wsShortDays) To UBound(wsShortDays)
            With wbk2.Worksheets(wsShortDays(SD))
                wbk2.Worksheets(wsShortDays(SD)).Activate
                Range("A:H").Copy
            End With
            Exit For
        Next SD


        wbk1.Worksheets(wsFullDays(FD)).Activate
        Range("C:J").PasteSpecial xlPasteAllUsingSourceTheme
        SD = 1
    End With
Next FD

1 Answer 1

2

You do not need the inner loop as your arrays are synced just use the same reference number from the first loop. It will equate Sunday with Sun and so forth:

Dim wsShortDays As Variant, wsFullDays As Variant
Dim FD As Long

wsShortDays = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
wsFullDays = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

For FD = LBound(wsFullDays) To UBound(wsFullDays)
   wbk2.Worksheets(wsShortDays(FD)).Range("A:H").Copy
   wbk1.Worksheets(wsFullDays(FD)).Range("C:J").PasteSpecial xlPasteAllUsingSourceTheme
Next FD
Sign up to request clarification or add additional context in comments.

2 Comments

you beat me to it! I had an explanation with mine also saying that excel isn't capable of stand-alone multi-threading which is why his nested for statements felt counter-intuitive to him. Our codes are 1:1 identical though.
Thanks Scott - I was really overcomplicating it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.