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,081,556
Messages
5,359,552
Members
400,533
Latest member
fpenning

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top