Inserting a row x number of times

alamode

New Member
Joined
Aug 21, 2011
Messages
10
Hello. Its my first post here ! :)

I need some help in VBA. Just started recently using it and finding it to be of great help. Im stuck on one problem though.

I need to insert rows in a sheet a certain number of times. To insert rows i used this command:

Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove

Which works alright. Problem is it runs only once. I need to run it multiple times.. So i did this:

For i = 1 To 19
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
Next i

And again it works alright.. But the problem is, I dont know exactly how many times im going to repeat this command. It is calculated in cell I10. I have a formula set in sheet1, Cell I10 which tells me that i need 19 rows. So i was hoping i could link this cell up so that it would repeat this command as many times as the value in Sheet1,Cell I10 is. Cant get to do this though.

Help me out !

Regards!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Nevermind !! I got it.. This code got it working !

A:
If Worksheets("Details for autocreation").Range("I10").Value > I Then
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
I = I + 1
GoTo A
Else
End If
End Sub
 
Upvote 0
I hate to see a VBA beginner using the GoTo statement as it can always be avoided. You were nearly there with your For loop. Try this:
Code:
    Dim i As Integer
    
    For i = 1 To Range("I10").Value
        Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    Next
Or without a loop, using the Resize method:
Code:
    Selection.Resize(Range("I10").Value, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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