Workbooks.Open fills in the "source", not the "target" file

I have tried the following, and many others have not noted, with limited success.

https://msdn.microsoft.com/en-us/library/office/ff194819.aspx

Workbooks.Open Method in VBA

My situation is this: I have a weekly file from our ERP system that fills in limited information. I have a macro recorded (yes, I know ...) to automatically fill in other data that I need related to another source, but the macro always asks me to open the "source" workbook before the data can fill ... I wanted to use the Open.Workbooks expression to open the workbook (read-only and not update links), so I didn't have to "expand" in the original workbook every time I run the macro.

I found a line that will open the book as a read-only file and won't update the links. I tested it as standalone and it works like a charm:

Workbooks.Open Filename:="SourceFile.xlsx", ReadOnly:=True, UpdateLinks:=False

      

The problem I'm running into is when I combine this little subroutine with another macro, the data to be populated in the "target" file is actually populated in the "source" file. I need to open the "source" document and then activate the "target" workbook so that the population of data meets in the "target" workbook and not the "source"

I have seen code snippets that are relevant to setting the active workbook to the "target" workbook after using the expression "open.workbooks", but I don't know how to implement them.

+3


source to share


1 answer


dim wbsource as workbook
dim wbtarget as workbook

set wbsource = activeworkbook
set wbtarget = Workbooks.Open Filename:="SourceFile.xlsx", ReadOnly:=True, UpdateLinks:=False

'do your code here

      



Set up two different workbook objects.

+1


source







All Articles