VBA & Macro Problems

WPS Spreadsheets VBA: Search External Workbook and Copy Matching Rows

Adam DavisAdam Davis Nov 13, 2026 869 views

Question details

The user needs to automate a VBA search in WPS Spreadsheets that opens an external workbook, qualifies all references to that workbook, finds rows matching criteria in columns 1 through 4, copies columns 1 through 18 for each matching row, and pastes them into a calculated destination row.

WPS Spreadsheets VBA: Search External Workbook and Copy Matching Rows
Product
WPS Spreadsheets
Device & OS
not provided
Scenario
A user is writing a VBA macro to extract matching rows from an external workbook into a destination worksheet.
Observed behavior
Without fully qualified references, the macro can search or write to the wrong workbook; opening the external workbook once and qualifying source and destination worksheet references prevents this.
Before you start

Confirm the external workbook path and worksheet name before running the macro, and make sure the destination workbook is open in WPS Spreadsheets. If you are storing the VBA code in a file, save it as a macro-enabled workbook format so macros can run.

Solution 1Recommended

Use Fully Qualified VBA References to Search an External Workbook and Copy Matching Rows

Use this when the macro must reliably open an external workbook once, test each source row against criteria in columns 1 through 4, copy columns 1 through 18 for matches, and paste into a destination row.

The key is to qualify every worksheet and range with the external workbook object, such as Wb2.Worksheets("Sheet1") and sourceWs.Range(sourceWs.Cells(i, 1), sourceWs.Cells(i, 18)). This prevents WPS Spreadsheets from accidentally searching or writing to the wrong workbook.

Declare row counters as Long rather than Integer, and close Wb2 only after the loop finishes.

1
Open the VBA editor

In WPS Spreadsheets, press Alt+F11 to open the VBA editor, then click Insert > Module to create a new module.

2
Declare objects and set the destination worksheet

At the top of the procedure, use Long for row counters and set the destination worksheet explicitly, for example: Dim Wb2 As Workbook, sourceWs As Worksheet, destWs As Worksheet, i As Long, destRow As Long.

3
Open the external workbook once and set the source worksheet

Before the loop, run Workbooks.Open to open the external workbook and assign the source sheet: Set Wb2 = Workbooks.Open("C:\YourFile.xlsx") and Set sourceWs = Wb2.Worksheets("Sheet1").

4
Loop, test columns 1 through 4, and copy columns 1 through 18

Inside the loop, test sourceWs.Cells(i, 1) through sourceWs.Cells(i, 4) for your criteria. When matched, copy sourceWs.Range(sourceWs.Cells(i, 1), sourceWs.Cells(i, 18)) and paste it into the destination worksheet at a calculated destination row.

5
Close the external workbook after the loop

After the loop finishes, close Wb2 once with Wb2.Close SaveChanges:=False. Do not close it inside each loop iteration.

Use Fully Qualified VBA References to Search an External Workbook and Copy Matching Rows
VBA external workbook search is now qualified: Use fully qualified source and destination references and close Wb2 outside the loop to avoid searching the wrong workbook.

Run Qualified External Workbook VBA Searches in WPS Spreadsheets

WPS Spreadsheets lets you run the same VBA workflow: open an external workbook once, fully qualify source and destination worksheet references, loop through rows, copy matching columns, and paste into the destination sheet.

  1. 1. Open WPS Spreadsheets and enable the VBA editor: Launch WPS Spreadsheets, open the destination workbook, and press Alt+F11 to access the VBA editor.
  2. 2. Insert a module and paste the qualified macro: Click Insert > Module, then paste a macro that uses Workbooks.Open, fully qualified sourceWs and destWs references, Long row counters, and copies matching rows.
  3. 3. Run the macro: Close the VBA editor, return to WPS Spreadsheets, click Developer > Macros, select the macro, and click Run.
  4. 4. Verify the copied matching rows: Check the destination worksheet for the rows copied from columns 1 through 18 and confirm the external workbook closes without saving.
Search external workbooks with qualified VBA references in WPS SpreadsheetsCompatible with Microsoft Excel .xls, .xlsx, and .xlsm files and macrosFree, lightweight Office suite with a familiar VBA editor and worksheet functionsOpen and process external workbooks without changing your existing Excel-based workflow
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro search or copy from the wrong workbook?

The most common cause is unqualified references. Use the external workbook object and source worksheet, for example Wb2.Worksheets("Sheet1").Cells(i, 1), and also qualify the destination worksheet explicitly before copying.

Should I open and close the external workbook for each row in the loop?

No. Open Wb2 once before the loop, test and copy the matching rows, and close it once after the loop with Wb2.Close SaveChanges:=False. Closing it during each iteration is slow and can cause the loop to fail.

Why use Long instead of Integer for VBA row counters?

Long is safer for row counts because WPS Spreadsheets and Excel worksheets can exceed the 32,767 limit of Integer. Declare row counters as Long to avoid overflow errors when looping through large sheets.

Can I get the same matching rows without using VBA in WPS Spreadsheets?

Yes. Open both workbooks and use the FILTER function with an external workbook reference, such as =FILTER([External.xlsx]Sheet1!A1:R100,([External.xlsx]Sheet1!A1:A100=D1)*([External.xlsx]Sheet1!B1:B100=E1)). The external workbook must remain open while the formula recalculates.