Runtime error 1004

All2Cheesy

Board Regular
Joined
Mar 4, 2015
Messages
127
Hi guys,

I've written a Macro that will copy what's in cells and then insert that X amount of times in the cells below(Being used to copy formulas). This works perfectly once I've unlocked the sheet, however, I receive the following error when trying to run this macro in a locked sheet: Runtime Error '1004' Insert method of range class failed.

I've coloured the section that highlights in debug mode in my code below. It is also critical that the sheet remains locked. Thank you in advance! :)

Code:
Sub AddLines()

Application.Calculation = Manual
Range("A5023:AG5023").Copy
[COLOR="#FF0000"]Range("A5023:AG5023").Resize(Range("Q7").Value).Insert Shift:=xlDown[/COLOR]
Application.Calculation = xlAutomatic

    Range("A5024:G65000").Select
    Selection.ClearContents
    Range("J5024:L65000").Select
    Selection.ClearContents
    Range("Q5024:T65000").Select
    Selection.ClearContents


End Sub
 

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.
The sheet will have to be unlocked unless you use the UserInterfaceonly method, which has to be applied at workbook-open and need to reference the sheet in question
See here for more info

Excel VBA: Macro Code To Run Macros On Protected Worksheets & Sheets

Code:
Sub AddLines()
Application.Calculation = Manual
Range("A5023:AG5023").Copy
Range("A5023:AG5023").Resize(Range("Q7").Value).Insert
Application.Calculation = xlAutomatic
    Range("A5024:G65000").ClearContents
    Range("J5024:L65000").ClearContents
    Range("Q5024:T65000").ClearContents
End Sub
 
Upvote 0
The sheet will have to be unlocked unless you use the UserInterfaceonly method, which has to be applied at workbook-open and need to reference the sheet in question
See here for more info

Excel VBA: Macro Code To Run Macros On Protected Worksheets & Sheets

Code:
Sub AddLines()
Application.Calculation = Manual
Range("A5023:AG5023").Copy
Range("A5023:AG5023").Resize(Range("Q7").Value).Insert
Application.Calculation = xlAutomatic
    Range("A5024:G65000").ClearContents
    Range("J5024:L65000").ClearContents
    Range("Q5024:T65000").ClearContents
End Sub


Ah okay.

Thanks for that. :)
 
Upvote 0
Found a work around in case you were interested.

Unlocking and then re-locking the sheet within the macro gets the job done a tad quicker. Thanks for the assist though!

Code:
Sub AddLines()

'Unlock Sheet
ActiveSheet.Unprotect Password:="PASSWORD"

Application.Calculation = Manual
Range("A5023:AG5023").Copy
Range("A5023:AG5023").Resize(Range("Q7").Value).Insert
Application.Calculation = xlAutomatic
    Range("A5024:G65000").ClearContents
    Range("J5024:L65000").ClearContents
    Range("Q5024:T65000").ClearContents
    
'Lock Sheet
ActiveSheet.Protect Password:="PASSWORD"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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