Insert Row + Formula Problem, please help!

excelstep

New Member
Joined
Jun 29, 2011
Messages
3
I have a drilldown list of numbers in cell B2 using data validation. Once user select number in cell B2, it will insert rows below the numbers. The codes I have used:

Sub Insertline()
Dim nRows As Long
Dim range("B2") As Long
range("B2").Select
nRows = range("B2").Value
ActiveCell.Offset(1, 0).Resize(nRows).EntireRow.Insert
End Sub

How do I insert formula in the new rows inserted below B2 since number of rows varies dependent on cell B2? Is there a formula like Do, Loop that can help? Or should I set variable?

Say I want a simple formula like this, I have this:

range("B3").Select
ActiveCell.Formula = range("B2") - (range("B2") - 1)
ActiveCell.Offset(1, 0).Select

But this doesnt work when I have user select different rows in b2.
Please help! Thank you very much!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi There,

Please try using the below code..

Code:
Sub Insertline()
    Dim nRows As Long
    nRows = range("B2").Value
    range("B3:B" & nRows + 2).EntireRow.Insert Shift:=xlDown
    range("B3").Value = range("B2").Value + range("B2").Value - 1
    range("B3:B" & nRows + 2).FillDown
'ActiveCell.Offset(1, 0).Resize(nRows).EntireRow.Insert
End Sub

Hope it will work.

Cheers,
Dine
 
Upvote 0
To copy a formula to next cell, can use:
Code:
Range("B2").Copy
Range("B2").Offset(1, 0).PasteSpecial xlPasteFormulas

Maybe you can use for loop to copy formulas for all the new rows??

lolo
 
Upvote 0
Thanks for your reply. However, how do I set a formula like I have my selected no of rows in B2, then I want B3 until the end to be filtered with numbers starting from 1?

4 <- I have selected "4" in B2
1
2
3
4

4 rows will be inserted with the numbering starting from 1 until the number of selection. Is there a formula, like If something that can help this?
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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