How to Insert Rows in Excel Based on Active Cell Value Using VBA
Question details
The user needs a VBA macro that reads a number from the currently selected (active) cell and dynamically inserts that exact number of new rows directly below it.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating bulk row insertion based on specific cell data using a VBA script.
- Observed behavior
- The user's original macro failed because it attempted to assign the active cell's numeric value to a Range object variable, leading to a type mismatch or incomplete execution.
Before running any new VBA macro, ensure you have saved a backup of your current workbook, as VBA actions usually cannot be undone using the standard Undo button.
Use a VBA Macro with a Numeric Variable
Use the Val() function to assign the active cell's value to a numeric variable (Long), then use the Offset and Resize methods to insert the rows.
By assigning the active cell's value to a variable defined as 'Long', you avoid the object mismatch error commonly caused by declaring it as a Range object. The 'Val()' function safely evaluates the cell contents, preventing crashes if the cell happens to be blank or contains text.
Once the numeric value is stored, the macro leverages the 'Offset' and 'Resize' properties to select the exact number of rows immediately below the active cell and inserts new blank rows in their place.
In your open Excel workbook, press the 'Alt + F11' keys simultaneously to launch the Microsoft Visual Basic for Applications (VBA) editor window.
Click on 'Insert' in the top menu bar of the VBA editor, then select 'Module' from the dropdown list to create a new blank script window.
Copy and paste the following code into the empty module window: Sub InsertRows() Dim NumberOfRows As Long NumberOfRows = Val(ActiveCell.Value) If NumberOfRows >= 1 Then ActiveCell.Offset(1).Resize(NumberOfRows).EntireRow.Insert End If End Sub
Close the VBA editor to return to your worksheet. Select a cell containing the number of rows you wish to insert, navigate to the 'Developer' tab, click 'Macros', select 'InsertRows', and click 'Run'.

Run VBA Macros Seamlessly with WPS Office
WPS Spreadsheet offers excellent compatibility with Microsoft Excel VBA macros. You can write, edit, and run your automated scripts directly within WPS Office to effortlessly manage tasks like dynamic row insertion.
- 1. Download WPS Office: Download and install WPS Office, ensuring you select the version that includes VBA support.
- 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing macro-enabled Excel workbook (.xlsm).
- 3. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor' to view or edit your macros.
- 4. Run the Macro: Select the target cell in your sheet, go back to the 'Developer' tab, click 'Macros', and execute your row insertion script perfectly.

Frequently Asked Questions
Why do I get a Type Mismatch error with my original VBA macro?
A 'Type Mismatch' error occurs if you try to assign the numeric value of the active cell to a variable declared as a 'Range' object instead of a numeric type. You must declare the variable as 'Long' or 'Integer' to properly store and process numeric row counts.
Can I undo the row insertion after running the macro?
No, running a VBA macro automatically clears your undo history in Excel, meaning you cannot use Ctrl+Z to revert the changes. Always save a backup of your workbook before running new macros.
What happens if the active cell is blank or contains text?
By using the 'Val(ActiveCell.Value)' function in the provided code, blank cells or text strings are evaluated as 0. Because the macro includes the safety condition 'If NumberOfRows >= 1', no rows will be inserted, preventing the script from crashing.




