How to Reference Source and Target Workbooks Correctly in Excel VBA
Question details
The user needs to learn how to reference separate source and target workbooks and worksheets in Excel VBA when opening and transferring data from an external file.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to read data from an external source workbook and write it to a specific target workbook.
- Observed behavior
- Needs the correct VBA syntax and object assignment methods to ensure data is read from and written to the correct files and sheets without relying on the unreliable ActiveWorkbook property.
Ensure you have the exact file path of the source workbook and that macros are enabled in your Excel trust center settings.
Use Explicit Workbook and Worksheet Variables
Assign each worksheet to its corresponding workbook variable to prevent VBA from acting on the wrong active workbook during the data transfer.
When transferring data between workbooks, relying on `ActiveWorkbook` or `ActiveSheet` can lead to errors because the active focus changes when a new file is opened. Explicitly declaring and setting your target (destination) and source variables ensures VBA knows exactly where to pull and push the data.
Set the target workbook to the one running the code by using the syntax `Set wbTarget = ThisWorkbook`.
Assign the specific target sheet by typing `Set wsTarget = wbTarget.Worksheets("TargetSheet")`, replacing 'TargetSheet' with your actual sheet's name.
Open the external file and assign it to a variable simultaneously using `Set wbSource = Application.Workbooks.Open("C:\Users\Location\SourceData.xlsm")`. Ensure the file path is correct.
Assign the source sheet using `Set wsSource = wbSource.Worksheets("SourceSheet")`. You can now safely copy or reference data between `wsSource` and `wsTarget` using explicit ranges.

Write and Execute VBA Macros in WPS Office
WPS Spreadsheet provides a built-in VBA editor (in supported versions) that is highly compatible with Microsoft Excel macros, allowing you to run your workbook referencing scripts effortlessly.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your main macro-enabled workbook.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon.
- 3. Open the VBA Editor: Click on 'Visual Basic' to open the code editor window.
- 4. Paste and Run Your Code: Insert a new module, paste your explicit workbook referencing code, and press F5 to execute the data transfer.

Frequently Asked Questions
Why am I getting a 'Subscript out of range' error when referencing the source workbook?
This error usually occurs if the file path is incorrect, the workbook isn't open, or the worksheet name is misspelled. Double-check your `Workbooks.Open` path and ensure the worksheet name in `Worksheets("Name")` exactly matches the tab name in the Excel file.
What is the difference between ThisWorkbook and ActiveWorkbook in VBA?
`ThisWorkbook` always refers to the workbook where the VBA code currently resides and is being executed. `ActiveWorkbook` refers to the workbook that is currently in focus on the user's screen. Using `ThisWorkbook` is much safer for target assignments to prevent accidental data modification.
How do I automatically close the source workbook after transferring data?
You can close the source workbook by adding the line `wbSource.Close SaveChanges:=False` at the end of your macro. This closes the file and discards any accidental changes made during the data transfer process.




