How to Use Excel VBA to Insert Rows and Calculate Custom Values
Question details
The user needs an Excel VBA macro to find specific rows labeled as 'Type 2', insert a copied row above them, update the new row's label to 'Type 1+2', and perform calculations across the adjacent data columns.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating row insertion, labeling, and column-wide data calculation in a spreadsheet based on specific text conditions.
- Observed behavior
- The goal is to automatically locate rows with 'Type 2', duplicate the preceding row, insert it above, update the label, calculate new values, and lock those values by removing formulas.
Ensure that the Developer tab is enabled in your spreadsheet program to access the VBA editor, and verify that your target data (such as the 'Type 2' labels) is consistently formatted within a specific column.
Create a VBA Macro to Loop Upward and Insert Rows
Use a bottom-up VBA loop to prevent row shifting issues when dynamically inserting new rows, updating text labels, and applying calculation formulas.
When dynamically inserting rows with a VBA loop, you must iterate from the bottom row upwards. This prevents the indices of unprocessed rows from shifting downward as new rows are inserted above them, ensuring every target row is evaluated and processed correctly.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor in your spreadsheet application.
Click on 'Insert' in the top menu bar and select 'Module' to create a blank script window for your new macro.
Initialize a loop starting from the last row going up, for example using the syntax 'For i = LastRow To 2 Step -1'. Inside the loop, check the specified column (e.g., Column B) for your 'Type 2' criteria.
When the condition is met, copy the preceding row (Row 'i - 1') and use the insert command (e.g., 'Insert Shift:=xlDown') to place it above the 'Type 2' row. Change the label in the newly inserted row to 'Type 1+2'.
Assign the required calculation formulas across the data columns for the new row. Convert them to static values by setting 'Range.Value = Range.Value', and finally execute 'Application.CutCopyMode = False' to clear the clipboard.

Automate Data Processing with WPS Spreadsheet
WPS Spreadsheet natively supports VBA macros, allowing you to automate repetitive tasks like inserting rows and calculating values with ease. Enjoy a complete Office experience with powerful macro support built directly into the software.
- 1. Open Your Workbook: Launch WPS Office and open your .xlsm or macro-enabled spreadsheet file.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon. If it is hidden, enable it in the customized ribbon settings.
- 3. Open the VBA Editor: Click the Visual Basic icon or press ALT + F11 to open the VBA editor and insert your customized macro code.
- 4. Run the Macro: Return to your active worksheet, click Macros, select your custom script for inserting rows, and hit Run to automate your workflow.

Frequently Asked Questions
Why does my Excel macro skip rows when inserting new ones?
If your macro loop runs top-to-bottom (e.g., from Row 1 to Row 100), inserting a row shifts all subsequent rows down, altering their row numbers. The macro then skips the next item because its index number was bypassed. Always use a bottom-up loop (Step -1) when inserting or deleting rows to prevent this issue.
How do I quickly replace formulas with calculated values using VBA?
You can replace formulas with static values in VBA without using copy-paste by setting the range value to itself. For example, using the syntax 'Range("C2:F2").Value = Range("C2:F2").Value' instantly removes the formulas and retains only the calculated numeric results.
How do I clear the clipboard in VBA after copying and pasting?
After completing your copy and paste commands in the code, add the line 'Application.CutCopyMode = False' to your VBA script. This instruction clears the system clipboard and removes the moving dotted line (marquee) around the cells you just copied.




