VBA & Macro Problems

Copy a Value After Inserting a New Row Using Excel Office Scripts

Kushani NimanthikaKushani Nimanthika Nov 11, 2026 868 views

Question details

The user needs to automate a process where a new row is inserted above the active cell, and the value from the first column of the original row is copied into the corresponding cell of the newly inserted row.

How to Copy a Value from the Original Row After Inserting a New Excel Office Script Row
Product
Excel
Device & OS
not provided
Scenario
Automating repetitive data entry or formatting tasks where newly inserted rows must inherit specific identifiers or category values from the rows they displace.
Observed behavior
The goal is to successfully execute a script that inserts a row and duplicates the column A value from the shifted row into the new one without manual copying.
Before you start

Ensure you have the Automate tab enabled in your Excel ribbon and possess the necessary Microsoft 365 permissions to create and run Office Scripts.

Solution 1Recommended

Use an Office Script to Insert the Row and Copy the Value

Create and run a custom Office Script that identifies the active cell, inserts a row, and copies the original data from the shifted row.

Office Scripts use TypeScript to automate tasks in Excel. When a new row is inserted above the active cell using the script, the original row is pushed down, meaning its index increases by 1. Therefore, the script must reference `row + 1` to locate and copy the original data accurately.

1
Open the Code Editor

Navigate to the Automate tab in your Excel ribbon and click on New Script to open the Code Editor pane on the right side of your screen.

2
Paste the Office Script

Delete any existing code in the editor and paste the following script: function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet(); const activeCell = workbook.getActiveCell(); const row = activeCell.getRowIndex(); activeCell.getEntireRow().insert(ExcelScript.InsertShiftDirection.down); sheet.getCell(row, 0).copyFrom(sheet.getCell(row + 1, 0), ExcelScript.RangeCopyType.all); }

3
Save the Script

Rename the script by clicking its title at the top of the Code Editor (e.g., 'Insert Row and Copy') and click Save.

4
Run the Script

Select the cell in your worksheet where you want the new row to be inserted, then click the Run button in the Code Editor pane.

Use an Office Script to Insert the Row and Copy the Value
Script Execution: Once executed, the active row will shift down, and column A of the new blank row will automatically populate with the value from the original row.
Automate Tasks in WPS Spreadsheet

Use WPS JS Macros to Automate Row Insertion and Copying

WPS Office Spreadsheet offers powerful JavaScript (JS) Macros and extensive VBA support to automate repetitive tasks like inserting rows and copying data. You can easily achieve the same automation offline and for free.

  1. 1. Open the Macro Editor: Open your workbook in WPS Spreadsheet, navigate to the Tools tab, and click on JS Macro to launch the WPS Macro Editor.
  2. 2. Write the Macro: Create a new module and write a script using the WPS JS API to duplicate the row insertion and copy logic (e.g., using `ActiveCell.EntireRow.Insert()`).
  3. 3. Execute the Macro: Save your code and run the macro directly from the editor or assign it to a custom button in your spreadsheet for one-click execution.
Fully compatible with Microsoft Excel (.xlsx, .xlsm) formats and macros.Built-in JS Macro and VBA editor for seamless, advanced automation.Lightweight software with a familiar, easy-to-use interface.Free to use without requiring an expensive enterprise subscription.
microsoft office alternative - wps office

Frequently Asked Questions

Why is the Automate tab missing in my Excel?

Office Scripts are primarily available for enterprise, educational, or specific business Microsoft 365 accounts. If you are using a personal account or an older desktop-only version of Excel, the Automate tab will not be available.

How do I copy the entire row instead of just the first column?

To copy the entire row rather than just column A, modify the script to target the full row range. Instead of `sheet.getCell(...)`, you would define ranges for the entire source row and destination row, and use the `copyFrom` method on those broader ranges.

Can I use this TypeScript Office Script in WPS Office?

WPS Office uses its own JS Macro API and fully supports traditional VBA. While Microsoft's TypeScript-based Office Scripts are specific to Microsoft 365, you can easily replicate this exact logic in WPS using its built-in JS Macro editor or via VBA.