Number of rows to insert - Macro

sveioen

New Member
Joined
Aug 3, 2007
Messages
15
Hello,

Hope someone can help me with this little one;

I want to insert a number of rows. The number I get from subtracting B1 in the current sheet, with A1 in "Sheet1". I then insert as many rows as the number says. How can I do this using a macro?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Something like this perhaps:
Code:
Sub Macro1()
Dim sh1 As Worksheet
Dim sh As Worksheet
Dim NoOfLines As Long
Set sh1 = Sheets("Sheet1")
Set sh = ActiveSheet
NoOfLines = sh.Range("B1") - sh1.Range("A1")
sh.Rows("1:" & NoOfLines).Insert shift:=xlDown
End Sub
 
Upvote 0
Thanks Lewiy!

I tried your code and it works, but I get an error at the end:

Code:
Dim sh1 As Worksheet
Dim sh As Worksheet
Dim NoOfLines As Integer

Set sh1 = Sheets("Information")
' Set sh3 = Sheets("Sheet3")
Set sh = Sheets("Sheet6")
NoOfLines = sh.Range("B30") - sh1.Range("N1")
sh.Rows("1:" & NoOfLines).Insert shift:=xlDown

Error:

Code:
Run-time error '6':
Overflow
 
Upvote 0
This one:

NoOfLines = sh.Range("B30") - sh1.Range("N1")

(Changed the range-values, but this is irrelevant I think)
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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