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,362
Office Version
365
Platform
Windows
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,082,280
Messages
5,364,210
Members
400,786
Latest member
ismi88

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top