VBA loop: added worksheet activation disables workbook close

Noodleski

Active Member
Joined
Nov 18, 2015
Messages
467
Hi all,

some background to my question:
I have several xlsx files in a folder and this code below is part of a larger macro to assemble data from these sheets into a management overview.
This code has run several times, without a glitch, but I was asked to make sure that the source files were saved with the same sheet activated. I added the marked line, but then the code no longer closed the active workbook before continuing, running into a 'you already have xxxx.xlsx opened. Do you want to open xxxx.xlsx again anyway' message.

Anyone an idea why the bolded line is interfering with the second line below?

As you might get from the code: I need to save the source files again, as some updating is done in the process as well. So the overview file feeds info to the source files.

Code:
    MyFile = Dir(MyPath & "*.xlsx")
    
    Do While Len(MyFile) > 0
        Set wkbSource = Workbooks.Open(MyPath & MyFile)
        Set wksSource = wkbSource.Worksheets("Sales Prognose")
        Set wksAdjust = wkbSource.Worksheets("VF")
        'change the source sheet name accordingly
        Dim copy As Range
        Set copy = Worksheets("Sales Prognose").Range("B8:B20")
        Dim copy2 As Range
        Set copy2 = Worksheets("Sales Prognose").Range("D8:O20")
        Dim copy3 As Range
        Set copy3 = Worksheets("Sales Prognose").Range("B2")
        ThisWorkbook.Activate
        Range("paste").Value = copy.Value
        Range("paste_2") = copy2.Value
        Range("paste_3") = copy3.Value
        
        wksAdjust.Range("C4:N16").Formula = "=SUMIFS(overview.xlsm!TSTSUD[to],overview.xlsm!TSTSUD[rep],'Sales Progn'!R2C2,overview.xlsm!TSTSUD[mnd],VF!R3C,overview.xlsm!TSTSUD[Prod],VF!RC1)"
        wksAdjust.Range("C4:N16").copy
        wksAdjust.Range("C4:N16").PasteSpecial xlPasteValues
[B]        Worksheets("Sales Prognose").Activate[/B]
        Application.CutCopyMode = False
        wkbSource.Close savechanges:=True
        'wkbSource.Close
        
        MyFile = Dir
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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