VBA Insert rows according to cell value AND number each new row

klight20

New Member
Joined
Jul 19, 2012
Messages
4
Hello, I have code that inserts a certain amount of rows according to a certain cells value:

Code:
Sub addrowsc()    
    Dim r, count As Range
    Dim LastRow As Long
    Dim temp As Integer
    Set r = Range("A:AM")
    Set count = Range("T22")
    LastRow = Range("T" & Rows.count).End(xlUp).Row
    For N = LastRow To 1 Step -1
        temp = Range("AI" & N)
        If (temp > 1) Then
            Rows(N + 1 & ":" & N + temp).Insert Shift:=xlDown
        End If
    Next N
End Sub

But I also want to number each row from 1 to value, so if I have the cell value set at 24, when the code is ran I want it to add 24 new rows, which it does, and I want it to number each row in column T according to the cell value, so from 1-24 for this example. Any help?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
The easiest way would be to add a new variable that will keep track of the count:
Code:
Sub addrowsc()    
    Dim r, count As Range
    Dim LastRow As Long
    Dim temp As Integer
    [COLOR="#FF0000"]Dim i As Long[/COLOR]
    Set r = Range("A:AM")
    Set count = Range("T22")
    LastRow = Range("T" & Rows.count).End(xlUp).Row
    [COLOR="#FF0000"]i = 1[/COLOR]
    For N = LastRow To 1 Step -1
        temp = Range("AI" & N)
        If (temp > 1) Then
            Rows(N + 1 & ":" & N + temp).Insert Shift:=xlDown
            [COLOR="#FF0000"]Range("T" & N + 1).Value = i
            i = i + 1[/COLOR]
        End If
    Next N
End Sub
 
Upvote 0
Thank you for the reply! I have had a delay in the project so have not been able to implement, but wanted to say thanks.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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