VBA & Macro Problems

How to Create a Dynamic Excel Template from Filtered Table Rows Using VBA

Adam DavisAdam Davis Oct 28, 2026 868 views

Question details

The user needs to dynamically copy visible rows from a filtered control table to a template worksheet and automatically update the print area based on the transferred data.

How to Create a Dynamic Excel Template from Filtered Table Rows Using VBA
Product
Excel
Device & OS
not provided
Scenario
Generating dynamic template reports (like addendums) by transferring filtered data from a master table and adjusting the page layout for printing.
Observed behavior
Automating the data extraction and print area adjustment to avoid manually copying data and resetting the print range every time the filter criteria change.
Before you start

Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and safely back up your data before running or testing new VBA scripts.

Solution 1Recommended

Use a VBA Macro to Copy Filtered Data and Update the Print Area

Implement a customized VBA script to dynamically copy only the visible rows from a filtered table and automatically adjust the worksheet's print area based on the new data dimensions.

This method involves two primary macros: one to clear the old template data and copy the newly filtered visible rows, and a second to calculate the last populated row and redefine the page setup's print area.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. Go to Insert > Module to create a new module for your macros.

2
Insert the Data Transfer Macro

Paste the following script to transfer visible cells: Sub UpdateAddendumTemplate() Dim DestRng As Range, SourceRng As Range Dim CTbl As ListObject With Sheets("CONTROL") Set CTbl = .ListObjects("Control") Set SourceRng = CTbl.ListColumns(1).DataBodyRange.Resize(CTbl.Range.Rows.Count - 1, 5).SpecialCells(xlCellTypeVisible) End With With Sheets("ADDENDUM TEMPLATE") Set DestRng = .Range("B42") DestRng.Resize(10000, 5).Clear End With SourceRng.Copy DestRng Call UpdatePrintArea MsgBox "Job Done!" End Sub

3
Insert the Print Area Automation Macro

Below the first macro, paste the routine that updates the print area based on the last row: Sub UpdatePrintArea() Dim LastRow As Long With Sheets("ADDENDUM TEMPLATE") LastRow = .Range("B:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row .PageSetup.PrintArea = "" .PageSetup.PrintArea = .Range("B1:F" & LastRow).Address End With End Sub

4
Run the Macro

Close the VBA editor. Filter your table on the 'CONTROL' sheet, then go to the Developer tab and click 'Macros'. Select 'UpdateAddendumTemplate' and click 'Run'. Your template and its print area will automatically update.

Use a VBA Macro to Copy Filtered Data and Update the Print Area
Macro Automation Tip: You can insert a Shape or Button on your 'CONTROL' sheet, right-click it, and select 'Assign Macro' to trigger the update with a single click.
Automate Data with WPS Office

Seamlessly Run VBA Macros in WPS Office

WPS Office Spreadsheet offers robust support for VBA macros and structured data tables, allowing you to run dynamic template scripts perfectly. Whether managing complex filtered lists or automating print setups, WPS provides a powerful environment to boost your productivity.

  1. 1. Open Your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm file containing the dynamic template logic.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon to access the VBA Editor and Macro manager.
  3. 3. Execute Your Scripts: Run your customized macros to instantly filter tables, generate templates, and align your print areas.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .csv) formatsBuilt-in advanced VBA support for automating workflows and templatesLightweight, fast execution, and exceptionally user-friendly interfaceFree alternative equipped with professional-grade spreadsheet tools
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA macro copying hidden rows instead of only the filtered ones?

To ensure only visible, filtered rows are copied, you must append `.SpecialCells(xlCellTypeVisible)` to your source range in the VBA script before executing the `.Copy` command.

How do I dynamically set the print area without using VBA?

You can achieve this by creating a dynamic Named Range using the OFFSET and COUNTA functions via the Name Manager. Once defined, go to Page Setup and enter the exact Named Range into the 'Print area' box.

Can I trigger the template generation macro using a drop-down list?

Yes. You can use a Data Validation drop-down to select a criterion, and use the `Worksheet_Change` event in VBA to automatically filter the table and call your update macro whenever the drop-down cell is modified.