Insert Multiple Rows based on cell value

IosifFlorin

New Member
Joined
Jul 1, 2015
Messages
18
Hello everyone!

need your help with some lines of code; i was looking for this on the forum, there are some posts about adding rows, but this is a bit more specific as it implies looping specific data.

> The number of rows to add: based on value in cells in column G

> Where to add the rows: below each cell in column G, the number of rows depending on what number is in each cell in column G

> Starting with cell G4

An example: in G4 we have as value 6 --> add below 6 rows
in G11 (before adding the rows above it was G5) we have as value 2 --> add below it 2 rows , and so on...

Thank you for your help!
 
When inserting or deleting rows (or columns) it is usually easiest to work backwards.
Try this in a copy of your worksheet.

Rich (BB code):
Sub Add_Rows()
  Dim r As Long
  
  Application.ScreenUpdating = False
  For r = Range("G" & Rows.Count).End(xlUp).Row To 4 Step -1
    If Cells(r, "G").Value > 0 Then Rows(r + 1).Resize(Cells(r, "G").Value).Insert
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
IosifFlorin,

Here is a macro solution for you to consider.

You can change the raw data worksheet name in the macro.

Sample raw data:


Excel 2007
G
1
2
3
46
52
6end
7
Sheet1


After the macro:


Excel 2007
G
1
2
3
46
5
6
7
8
9
10
112
12
13
14end
15
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub InsertRowsOnValue()
' hiker95, 10/21/2015, ME895532
Dim r As Long, lr As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, "G").End(xlUp).Row
  For r = lr To 4 Step -1
    If IsNumeric(.Cells(r, "G")) And .Cells(r, "G") > 0 Then
      .Rows(r + 1).Resize(.Cells(r, "G").Value).Insert
    End If
  Next r
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the InsertRowsOnValue macro.
 
Upvote 0
IosifFlorin,

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top