Insert Row based on cell value VBA

mgc8cc

New Member
Joined
Jan 24, 2020
Messages
6
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
All,

I want my formula to insert X amount of rows as the value in column 9 states. (E.g. if row 2 column 9 had value = 3, then I want to run the macro to insert 3 + 1 rows) Currently its putting a value in every row of column 9.

VBA Code:
Sub InsertRow()
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 9).End(xlUp).Row

For i = LastRow To 2 Step by - 1
A = Worksheets("Clinic & Resource Alignment").Cells(i, 9).Value
For j = 1 To A

Worksheets("Sheet1").Rows(i + 1).Select
Selection.Insert Shift:=xlDown
   
        Next
Next
Range("G2:J2").Select
Selection.AutoFill Destination:=Range("G2:J240"), Type:=xlFillDefault
Range("G2:J240").Select
   

   
Worksheets("Sheet1").Cells(1, 1).Select
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
All,

I want my formula to insert X amount of rows as the value in column 9 states. (E.g. if row 2 column 9 had value = 3, then I want to run the macro to insert 3 + 1 rows) Currently its putting a value in every row of column 9.



VBA Code:

Sub InsertRow()
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 9).End(xlUp).Row

For i = LastRow To 2 Step by - 1
A = Worksheets("Sheet1").Cells(i, 9).Value
For j = 1 To A

Worksheets("Sheet1").Rows(i + 1).Select
Selection.Insert Shift:=xlDown

Next
Next
Range("G2:J2").Select
Selection.AutoFill Destination:=Range("G2:J240"), Type:=xlFillDefault
Range("G2:J240").Select



Worksheets("Sheet1").Cells(1, 1).Select
End Sub
 
Upvote 0
i feel like this is your answer but instead of "with cells (i, 1)" it would be with "cells(i, 9)"
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,212
Members
448,874
Latest member
b1step2far

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