VBA Insert rows

candacem

New Member
Joined
Sep 8, 2017
Messages
18
Hi Everyone,

I have been working on a string and having issues. I have the basic set out, but I want to add in where the rows should be inserted, not at cell A1, but starting at cell A8.

Sub MyInsertRows1()

Application.DisplayAlerts = False

Dim i As Integer

For i = 1 To Range("d1").Value
Rows(1).Insert
Next i

Application.DisplayAlerts = True


End Sub

I'm still learning VBA so please be kind!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try

Code:
Sub MyInsertRows1()
Application.DisplayAlerts = False
 Dim i As Integer
 For i = 1 To Range("d1").Value
 Range("A8").EntireRow.Insert
 Next i
 Application.DisplayAlerts = True

 End Sub
 
Upvote 0
Try:
Code:
Sub InsertRows()

    If Range("D1").Value > 0 Then Range("A8").Resize(Range("D1").Value).EntireRow.Insert
    
End Sub
 
Last edited:
Upvote 0
That's the error message, not what line of code it occurs on.

Both suggestions run fine for me, without that error message.
 
Upvote 0
That fixed it, but I didn't realize that I needed to be "below" A8 (A8 is a header to the Account Number) - As below. My end goal is to have a button to update the account numbers (replacing the text that is currently appear as below) based off of the cost centre. So step one is to get the macro to insert the appropriate amount of rows to accommodate the second step of the formula to look up the account numbers associated with the cost centre...I have that gives a bit more clarity to the end goal.

[TABLE="width: 272"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Acct Number[/TD]
[TD]Description[/TD]
[/TR]
[TR]
[TD]Salaries[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Benefits[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Building Expense - Non-Staff[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Rent[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Advertising & Promotion[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Field Support Functions[/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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