VBA & Macro Problems

How to Create an Excel VBA Macro to Repeat Rows and Distribute Values

Aamir Naveed AkramAamir Naveed Akram Nov 3, 2026 869 views

Question details

The user needs a VBA macro to insert 8 blank rows below each existing row in a 1,680-row dataset, copy the values of columns A through C into the new rows, and distribute the original column D value diagonally across columns E through L.

How to Create an Excel VBA Macro to Repeat Rows and Distribute Values
Product
Microsoft Excel
Device & OS
not provided
Scenario
Restructuring a large, flat dataset into an expanded, diagonally staggered format using automated VBA scripting.
Observed behavior
The goal is to automatically execute the row insertion and cell distribution without manual copying, ensuring that the script processes all 1,680 rows accurately.
Before you start

Ensure you save a backup copy of your workbook before running the macro, as VBA operations cannot be undone using the standard Excel Undo feature.

Solution 1Recommended

Write a Bottom-Up VBA Macro Script

Process the rows from the bottom (row 1,680) to the top (row 1). This ensures that inserting new rows does not shift the position of the unprocessed data below it.

To accomplish this task, the VBA script must loop backwards. If you loop forwards, inserting rows will push the next target row down, breaking the loop logic. The macro will also utilize the Offset function to handle the diagonal placement of column D's value into columns E through L.

1
Open the VBA Editor

Press 'Alt + F11' on your keyboard to open the Visual Basic for Applications editor.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a blank script window for your code.

3
Write the Optimization Code

At the beginning of your Sub routine, add 'Application.ScreenUpdating = False', 'Application.Calculation = xlCalculationManual', and 'Application.EnableEvents = False'. This prevents Excel from freezing while processing the 1,680 rows.

4
Create the Reverse Loop

Set up a For loop that counts backward: 'For i = 1680 To 1 Step -1'. Inside this loop, use 'Rows(i + 1 & ":" & i + 8).Insert' to add the 8 required blank rows below the current row.

5
Copy Columns A to C

Still inside the loop, copy the first three columns using 'Range("A" & i & ":C" & i).Copy' and paste them into the 8 newly created rows.

6
Distribute Column D Diagonally

Store the value of column D in a variable. Create a smaller loop from 1 to 8, and use 'Cells(i + j, 4 + j).Value = yourVariable' (where j is the step 1 to 8) to place the value diagonally into columns E through L.

7
Run the Macro

Restore the Application settings to True at the end of the script, click anywhere inside your code, and press 'F5' to run the macro.

Write a Bottom-Up VBA Macro Script
Performance Boost: By disabling screen updating and automatic calculations before the loop, the macro execution time drops from several minutes to just a few seconds.
Advanced Macros in WPS

Automate Complex Data Tasks with WPS Office

WPS Spreadsheets provides robust support for VBA macros, allowing you to run complex data restructuring scripts to repeat rows and distribute values efficiently.

  1. 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
  2. 2. Open Your Workbook: Launch WPS Spreadsheets and open the workbook containing your 1,680 rows of data.
  3. 3. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click 'Macros' or press 'Alt + F11' to open the VBA editor.
  4. 4. Run the Script: Paste your reverse-looping VBA script into a new module and hit 'Run' to automate your row distribution.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled workbooksBuilt-in advanced VBA editor for writing and debugging scriptsOptimized performance for running loops on large datasetsFree to use with a lightweight installation and familiar interface
microsoft office alternative - wps office

Frequently Asked Questions

Why must the VBA macro process rows from bottom to top?

When inserting rows, the row numbers below the insertion point shift downwards. By looping backwards (e.g., from row 1,680 down to 1), the unprocessed rows above remain at their original indices, preventing the script from skipping data or entering an infinite loop.

How can I speed up a VBA macro that inserts thousands of rows?

You can dramatically improve performance by disabling screen updates, automatic calculations, and events before your loop begins. Use the code Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual at the start, and ensure you turn them back on at the end.

How do I distribute a value diagonally using VBA?

Diagonal distribution is achieved using the Offset property or dynamic cell referencing. By incrementally increasing both the row index and the column index simultaneously inside a loop (e.g., shifting down 1 row and right 1 column per step), the target cell moves diagonally.

Are Excel VBA macros compatible with WPS Office?

Yes, WPS Spreadsheets supports standard VBA macros. You can open your Excel .xlsm files in WPS Office, access the built-in VBA editor via the Tools tab, and run your scripts without needing to rewrite the code.