Trouble switching between workbooks after Workbooks.Open(sPath)

onetap

New Member
Joined
Sep 25, 2017
Messages
13
I have the following code which appears to get stuck after I open a new workbook (EMEA). I would like to be able to filter the first workbook which is already open (data), and copy and paste between the two. As soon as I open the new workbook, I cannot navigate back to the data workbook and the code doesn't run as it should. When I step through the code to debug it, it works normally which is weird (although I have to manually click back to the data workbook first). I'm calling Sub copy() from another sub command if that makes a difference and the macro is saved in PERSONAL.XLSB

Code:
    Public wb As Workbook
    Public sPath As String
    Public LR As Long

    Sub copy()

    today = Format(Date, "mm-dd-yyyy")

    sPath = "S:\[I]XXXXX\EMEA[/I].xlsx"
    Set wb = Workbooks.Open(sPath)

    Windows("Data (" & today & ").xlsx").Activate
    ActiveSheet.ShowAllData

    ActiveSheet.Range("$A$1:$CU$20000").AutoFilter Field:=4, Criteria1:="EMEA"
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
        Range("D2:D" & LR).Select
        Selection.Copy

    Windows("EMEA.xlsx").Activate
        Range("A3").Select
        ActiveSheet.Paste
    [\code]


Thanks in advance!
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Does this work?
Code:
Sub copy()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim sPath As String
Dim LR As Long
Dim today As String

    Set wbSrc = ActiveWorkbook
    
    today = Format(Date, "mm-dd-yyyy")

    sPath = "S:\XXXXX\EMEA.xlsx"
    
    Set wbDst = Workbooks.Open(sPath)
    
    With wbSrc.ActiveSheet
        .ShowAllData
        .Range("$A$1:$CU$20000").AutoFilter Field:=4, Criteria1:="EMEA"
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        .Range("D2:D" & LR).copy wbDst.ActiveSheet.Range("A3")
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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