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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
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
I get an error with that one :(

Run-time error '1004' : Application-defined or object-defined error
 
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
Is the sheet protected?
 
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.

Acct NumberDescription
Salaries
Benefits
Building Expense - Non-Staff
Rent
Advertising & Promotion
Field Support Functions

<colgroup><col><col></colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,664
Members
449,045
Latest member
Marcus05

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