Activate Another Workbook Not Working

Oprichnick

Board Regular
Joined
May 30, 2013
Messages
69
Hello I have a vba routine which was performing well, but somehow stopped working and I can't figure out why.

A have a macro in a workbook that is used to break down another workbook called "Report" (xlsx) in several excel sheets. Although there is a workbook activation, it is breaking the first file (which contain the code) which I call "Splitter".


Code:
Sub Splitting()

Dim xPath As String
xPath = Application.ActiveWorkbook.Path


Workbooks("Report.xlsx").Activate


Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
    xWs.Copy
    Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xls"
    Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True




MsgBox ("The Splitting is Done")
End Sub

Thanks for your help,
Oprichnick
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You're not looping through the sheets of the active workbook, instead you're looping through the sheets of ThisWorkbook where the macro is being called from. Instead, try:
Code:
Sub Splitting_v1()

    Dim wks     As Worksheet
    Dim strPath As String
    
    strPath = ActiveWorkbook.path & "\@1.xls"
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    
    With Workbooks("Report.xlsx")
        For Each wks In .Sheets
            wks.Copy
            With ActiveWorkbook
                .SaveAs Replace(strPath, "@1", wks.Name)
                .Close False
            End With
        Next wks
    End With
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

    MsgBox("The Splitting is Done")

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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