logo
search
VBA & Macro Problems

How to Merge Data, Autofill Formulas, Format and Sort Using VBA

Anum MushtaqAnum Mushtaq Oct 14, 2026 872 views

Question details

The user wants to automate a workflow using VBA to identify missing values, add new rows, autofill specific formulas (columns M:R), format column R, and sort the data in column L.

How to Merge Data, Autofill Formulas, Format Rows, and Sort Values with VBA
Product
WPS Spreadsheet / Microsoft Excel
Device & OS
not provided
Scenario
Automating data compilation, formula population, formatting, and sorting tasks using macros.
Observed behavior
The user needs a functional VBA method to sequentially append missing data to the next empty cell, copy down adjacent formulas, apply specific cell colors, and alphabetically sort the updated range.
Before you start

Before running any VBA macro, ensure you have saved a backup of your workbook, as macro actions cannot be undone using the standard Undo feature.

Solution 1Recommended

Write a VBA Macro to Automate Data Processing

Use a customized VBA script with fully qualified references to process the data row by row, fill formulas, apply formats, and sort the final dataset.

Using fully qualified references in VBA ensures that the code modifies the exact worksheet intended. The script will identify the last used row, append the missing values in column L, drag down formulas for columns M through R, apply background color, and execute a sort method.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

Click on Insert in the top menu bar, then select Module to create a blank script window.

3
Find the Next Empty Row

Define your variables and use `wsDest.Cells(wsDest.Rows.Count, "L").End(xlUp).Row + 1` to locate the exact next empty cell in column L.

4
Insert Values and Autofill Formulas

Write the code to copy missing values into column L, then use the `.AutoFill` method to drag formulas from columns M:R down into the newly created rows.

5
Apply Formatting

Target column R and modify the `.Interior.Color` property of those specific cells to apply the desired background fill color.

6
Sort the Data

Use the `Range.Sort` method on column L to sort the newly merged region alphabetically in ascending order.

Write a VBA Macro to Automate Data Processing
Enable Developer Tab: Make sure you have enabled the Developer tab in your spreadsheet software's options menu so you can easily access the VBA editor and manage your macros.
Efficiently manage macros and data

Automate Your Spreadsheets with WPS Office

WPS Office provides robust support for VBA and Macros, allowing you to seamlessly automate tasks like merging data, autofilling formulas, and formatting cells just as you would in Microsoft Excel.

  1. 1. Download and Install: Download and install WPS Office on your computer.
  2. 2. Open Your Spreadsheet: Open your .xlsm or .xlsx file using WPS Spreadsheet.
  3. 3. Access the Developer Tools: Go to the Developer tab on the ribbon and click on 'Macros' or 'Visual Basic' to paste your VBA code.
  4. 4. Run Your Macro: Execute the macro to automatically merge data, autofill your formulas, format cells, and sort your spreadsheet.
Fully compatible with Microsoft Excel VBA scripts and macro-enabled workbooks (.xlsm)Free, lightweight, and fast installationIntuitive Developer tools for creating, editing, and debugging macrosAll-in-one suite with seamless switching between documents, spreadsheets, and presentations
microsoft office alternative - wps office

Frequently Asked Questions

Why isn't my VBA AutoFill code working for formulas?

Ensure the source range for the AutoFill exactly matches the starting cells of the destination range. If there's a mismatch in the range dimensions or the starting point, VBA will return a runtime error.

How do I sort a specific column alphabetically in VBA?

You can use the `Sort` method on your data range. Specify `Key1` as the range of the column you want to sort (e.g., Column L) and set the `Order1` parameter to `xlAscending`.

Does WPS Spreadsheet support VBA macros?

Yes, WPS Office Pro and specific regional versions fully support VBA macros. Ensure you have the VBA module installed or enabled within your WPS Office settings to use the Visual Basic Editor.