When running the code below, I get different results depending on which workbooks are open. The sub is in a module associated with Master Sheet.xlsm
If just the
Master Sheet.xlsm
open then the code runs correctly, i.e. the Message Boxes say (where comma separates the first and second message box): Master Sheet, transferred cases 03-09-18If both
Master Sheet.xlsm
andtransferred cases 03-09-18.xlsx
are open buttransferred cases 03-09-18.xlsx
was open second then the message boxes say: transferred cases 03-09-18, transferred cases 03-09-18If both
Master Sheet.xlsm
andtransferred cases 03-09-18.xlsx
are open butMaster Sheet.xlsm
was open second then the message boxes say: Master Sheet, Master Sheet
Sub foo()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("C:\Users\owner\Documents\ExelatecOutput\Master Sheet.xlsm")
Set y = Workbooks.Open("C:\Users\owner\Documents\ExelatecOutput\transferred cases 03-09-18.xlsx")
'Now, copy what you want from x:
MsgBox x.Name
MsgBox y.Name
End Sub
Why do the variables x and y not get assigned correctly.