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:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,275
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
 

Forum statistics

Threads
1,078,138
Messages
5,338,465
Members
399,234
Latest member
WaddoAU

Some videos you may like

This Week's Hot Topics

Top