I have a macro that opens a new Workbook and then activate (focus) to first Workbook.
Code:
Set mainWorkbook = ActiveWorkbook
Set bdWorkbook = Workbooks.Open(FileName:="Another.xlsm", ReadOnly:=True)
mainWorkbook.Activate
I've got this code working in Excel 2007, but I've encountered an issue with the open workbook in Excel 2010 and later. The problem happens because Workbooks.Open returns to VBA before Excel Activate the new workbook [it works fine using debugger].
I can make an workarround using a Application.Wait (Now + TimeValue("0:00:01"))
, but......
EDIT: My code that dosen't work in Excel 2016
Sub Sample()
Dim path As String
path = "A_PATH_FROM_MY_SERVER"
actualScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
Set MainWB = ActiveWorkbook
Workbooks.Open fileName:=path, UpdateLinks:=False, ReadOnly:=isReadOnly
Set bdWB = ActiveWorkbook
DoEvents
MainWB.Activate
Application.ScreenUpdating = actualScreenUpdate
With Sheets(MY_BD_SHEET)
bdWB.ActiveSheet.UsedRange.Copy .[A1]
'....
End With
End Sub
DoEvents
between the last two lines? Otherwisebut....
what? – Scott HoltzmanActivate
it? Not sure if you have see THIS – Siddharth Rout