VBA & Macro Problems

How to Add Columns with Text using an Excel VBA Macro

Rana GarciaRana Garcia Nov 3, 2026 871 views

Question details

The user needs an Excel VBA macro to automatically append three new columns (Place, Location, and Code) to the end of an existing data range and populate them with specific, predefined text for every row.

How to Add Columns with Text using an Excel VBA Macro
Product
Excel
Device & OS
not provided
Scenario
Automating repetitive data entry tasks where specific default columns and text values need to be appended to an existing table without manually typing them out.
Observed behavior
The user wants a macro that can dynamically detect the current dataset's size, insert the three specific column headers at the end, and fill the subsequent rows with custom placeholder text.
Before you start

Ensure your data starts from cell A1 with no empty rows or columns breaking the table, and remember to save your workbook as a Macro-Enabled Workbook (.xlsm) before running the code.

Solution 1Recommended

Use a VBA Macro to Append Columns and Fill Text

Run this VBA code to automatically detect your current data range, insert the required headers, and fill the subsequent rows with custom text.

This macro dynamically calculates the total number of rows and columns in your dataset starting from cell A1. It uses the Offset and Resize properties to place headers exactly one column to the right of your existing data, and then fills the rows below with the text you specify.

1
Open the VBA Editor

Press ALT + F11 on your keyboard while in Excel to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click on 'Insert' in the top menu bar and select 'Module' to create a new blank script window.

3
Paste and Customize the Code

Copy the following code and paste it into the module window: Sub Add3Columns() Dim myTable As Range Dim tR As Long, tC As Long Set myTable = Range("A1").CurrentRegion tR = myTable.Rows.Count tC = myTable.Columns.Count With myTable .Offset(0, tC).Resize(1, 3).Value = Array("Place", "Location", "Code") .Offset(1, tC).Resize(tR - 1, 1).Value = "First column text" .Offset(1, tC + 1).Resize(tR - 1, 1).Value = "Second column text" .Offset(1, tC + 2).Resize(tR - 1, 1).Value = "Third column text" End With End Sub Be sure to replace 'First column text', 'Second column text', and 'Third column text' with the actual text you need.

4
Run the Macro

Close the VBA editor to return to your worksheet. Press ALT + F8 to open the Macro dialog box, select 'Add3Columns' from the list, and click 'Run'.

Use a VBA Macro to Append Columns and Fill Text
Tip for Success: Always test new macros on a copy of your dataset first to ensure the text is placed in the correct columns.
Advanced Automation in WPS

Automate Your Spreadsheets with WPS Office

WPS Spreadsheet provides powerful tools for managing large datasets and supports VBA macros in its advanced editions, making it easy to run automated scripts just like you would in Microsoft Excel.

  1. 1. Open Your Workbook in WPS Spreadsheet: Launch WPS Office and open your .xlsm file containing your dataset.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If you do not see it, go to Menu > Options > Customize Ribbon to enable it.
  3. 3. Open the Macro Editor: Click on 'Macros' or press ALT + F8, type a name for your macro, and click 'Create' to open the VBA editor.
  4. 4. Paste and Execute: Paste the provided Add3Columns VBA code into the editor, modify your text, and press F5 to run the macro.
Seamlessly run standard VBA macros to automate complex data entry tasks.Fully compatible with Microsoft Excel (.xlsx and .xlsm) file formats.Lightweight software that loads large datasets quickly and efficiently.Features a familiar, easy-to-use interface for quick adaptation.
microsoft office alternative - wps office

Frequently Asked Questions

How do I change the text inserted into the new columns?

In the provided VBA code, locate the lines ending with `.Value = "First column text"`. Replace the text inside the quotation marks with your desired predefined text. Ensure you keep the quotation marks around your new text.

What happens if my data does not start at cell A1?

The macro uses `Range("A1").CurrentRegion` to detect the table boundaries. If your data starts elsewhere (for example, at cell C5), change "A1" in the code to "C5" so the macro correctly identifies your dataset.

Can I add more than three columns using this macro?

Yes. To add a fourth column, change `Resize(1, 3)` to `Resize(1, 4)`, add a fourth header to the `Array("Place", "Location", "Code", "NewHeader")`, and duplicate the last row offset line, incrementing `tC + 2` to `tC + 3`.