VBA & Macro Problems

How to Create a VBA Macro to Copy Nonblank Rows into an Excel Table

Maira MehtabMaira Mehtab Nov 11, 2026 869 views

Question details

The user needs a VBA macro to selectively copy rows from a specific range into an existing Excel table only if a certain column is not blank, while appending a specific cell's value to an extra column in that table.

How to Create a VBA Macro to Copy Nonblank Rows into an Excel Table
Product
Excel
Device & OS
not provided
Scenario
Automating data consolidation where qualifying rows (non-blank in column E) from a source sheet need to be appended to a master table on a different sheet.
Observed behavior
Requires a programmatic VBA script to loop through rows, check for empty cells, dynamically add a ListRow to a ListObject table, populate it with data, and manage screen updating for performance.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and the Developer tab is visible. Verify the exact names of your source worksheet, destination worksheet, and target table before modifying the code.

Solution 1Recommended

Write a VBA Macro Using ListRows and For Loops

Use a VBA loop to iterate through the source rows, check for blanks, and dynamically add new rows to the destination ListObject table.

In VBA, tables are treated as ListObjects. To add a new row to an existing table, you use the ListRows.Add method. By looping through the target range and checking if the cell is empty using IsEmpty or checking for a null string, you can filter out blank rows.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications Editor. Click 'Insert' in the top menu and select 'Module' to create a new blank script window.

2
Set Up Screen Updating and the Loop

Start your macro by defining the Sub. Add 'Application.ScreenUpdating = False' to prevent the screen from flashing. Create a For loop that iterates from 7 to 12 (e.g., 'For i = 7 To 12').

3
Validate Nonblank Rows

Inside the loop, write an IF statement to check column E: 'If Trim(Worksheets("Calc").Cells(i, "E").Value) <> "" Then'. This ensures only rows with data in column E are processed.

4
Add a Table Row and Copy Data

Within the IF block, declare a new row object: 'Set newRow = Worksheets("MyTotal").ListObjects("TblTest").ListRows.Add'. Assign the values from columns E-I to the first 5 columns of the new row, and assign the value of Worksheets("Calc").Range("F16") to the 6th column.

5
Restore Screen Updating

Close your IF block and Next loop. Finish the macro by adding 'Application.ScreenUpdating = True' to turn the screen refresh back on.

Write a VBA Macro Using ListRows and For Loops
Best Practice: Using explicit worksheet references (like Worksheets("Calc")) ensures the macro runs correctly regardless of which sheet is currently active when the script is triggered.
Advanced Spreadsheet Automation

Automate Data Workflows with WPS Spreadsheet Macros

WPS Office provides robust support for VBA and macros, allowing you to easily automate repetitive tasks like copying nonblank rows into tables.

  1. 1. Install WPS Office: Download and install WPS Office on your computer.
  2. 2. Enable the Developer Tab: Open your spreadsheet, go to the top ribbon, and access the Developer tab to find macro tools.
  3. 3. Write and Run Code: Click 'Visual Basic', insert your module, paste the code to copy nonblank rows, and click 'Run'.
Highly compatible with Microsoft Excel VBA scripts and macro (.xlsm) formats.Lightweight application with incredibly fast script execution speeds.Built-in Developer tab gives you instant access to the Visual Basic Editor.Free to download and use with a highly familiar user interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why am I getting a 'Subscript out of range' error when running this macro?

This error occurs when the VBA code cannot find the specified worksheet or table. Check your workbook to ensure the sheet names exactly match 'Calc' and 'MyTotal', and the table is actually named 'TblTest' without any leading or trailing spaces.

How do I run this macro automatically by clicking a button?

Go to the Developer tab, click on 'Insert', and choose the 'Button' form control. Draw the button anywhere on your sheet. An 'Assign Macro' dialog box will appear; select your newly created macro from the list and click OK.

Can I use Microsoft Excel VBA code in WPS Office?

Yes, WPS Office Pro and advanced editions offer extensive support for Microsoft Excel VBA. Most standard VBA scripts, including ListObject manipulations, will run seamlessly in the WPS Spreadsheet environment without modification.