Add Rows one by one (based on a number in a cell)

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I have a CountA formula in:

iRow = Sheet2.Range("B12").Value

In Sheet9 starting in Row 15 I want to Insert a new Row. But I need to do this one at a time. If my value in iRow is 12 I need do this 12X times. I cannot just insert 12 rows at one time because I have a table below. I get an error message if there isnt already 12 empty rows between Row15 and where the second table starts. Therefore I have to loop through - adding a new row one by one.

Any help is appreciated

I was using this code, but as I stated if there arent enough blank rows between row 15 and the second table, this code doesnt work

Code:
Sub InsertRows()

Dim iRow As Long

    iRow = Sheet2.Range("B12").Value
    
     '   If Sheet2.Range("B12").Value > 0 Then
        
        Sheet9.Select
    
          Range("A15").EntireRow.Offset(1).Resize(iRow).Insert Shift:=xlDown
    
      '  Else
    
'End If
    
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Solved

Code:
Sub LoopRows()

Dim iRows As Integer
Dim iCount As Integer

'Select the current row
Sheet9.Range("A16").Activate
ActiveCell.EntireRow.Select

On Error GoTo Last
iRows = Sheet2.Range("B12").Value

'Keep on inserting rows until we reach the input number
For iCount = 1 To iRows
Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove
Next iCount
Last: Exit Sub



End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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