Copy and paste with unlock and lock worksheet

Project217

New Member
Joined
Mar 7, 2022
Messages
24
Office Version
  1. 365
Platform
  1. MacOS
Hi all.

Im Just having some trouble with the below VBA, works the first time perfectly but every second time it crashes and comes up with run time error 1004 PasteSpecial method of range class failed.
Also, I'm looking to add to this macro an email alert with just text only,
Any help would be awesome.

Sub PastTableToMasterSheet()
ActiveSheet.Unprotect Password:=""
Range("Table34").Copy
Sheets("Master Sheet").Unprotect Password:=""
Sheets("Master Sheet").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulas
Sheets("Master Sheet").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
Application.CutCopyMode = False
Rows("26:26").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.DELETE Shift:=xlUp
Range("B12").Select
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
MsgBox "Order Sent"
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try it
VBA Code:
On error resume next 
Sheets("Master Sheet").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulas
 
Upvote 0
Try
VBA Code:
Sub PastTableToMasterSheet()
    ActiveSheet.Unprotect Password:=""
    Range("Table34").Copy
    Sheets("Master Sheet").Unprotect Password:=""
    Sheets("Master Sheet").Range("B" & Sheets("Master Sheet").Cells(Rows.Count, 2).End(xlUp).Row).Offset(1, 0).PasteSpecial xlPasteFormulas
    Sheets("Master Sheet").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    Application.CutCopyMode = False
    Rows("26:26").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Range("B12").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    MsgBox "Order Sent"
End Sub
 
Upvote 0
Try
VBA Code:
Sub PastTableToMasterSheet()
    ActiveSheet.Unprotect Password:=""
    Range("Table34").Copy
    Sheets("Master Sheet").Unprotect Password:=""
    Sheets("Master Sheet").Range("B" & Sheets("Master Sheet").Cells(Rows.Count, 2).End(xlUp).Row).Offset(1, 0).PasteSpecial xlPasteFormulas
    Sheets("Master Sheet").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    Application.CutCopyMode = False
    Rows("26:26").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Range("B12").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    MsgBox "Order Sent"
End Sub
No, Its still the same. works even second time.
 
Upvote 0
Ok, So when I comma out the protection, It works even time, But when the unprotected and protect the ("Master Sheet") it only works every second time, Any thought's on what this could be?
 
Upvote 0
Hi
Got it
This will works for sure
VBA Code:
Sub PastTableToMasterSheet()
    ActiveSheet.Unprotect Password:=""
    Sheets("Master Sheet").Unprotect Password:=""
    Range("Table34").Copy
    Sheets("Master Sheet").Range("B" & Sheets("Master Sheet").Cells(Rows.Count, 2).End(xlUp).Row).Offset(1).PasteSpecial -4123
    Sheets("Master Sheet").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    Application.CutCopyMode = False
    Rows("26:26").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Range("B12").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    MsgBox "Order Sent"
End Sub
 
Upvote 0
Hi
Got it
This will works for sure
VBA Code:
Sub PastTableToMasterSheet()
    ActiveSheet.Unprotect Password:=""
    Sheets("Master Sheet").Unprotect Password:=""
    Range("Table34").Copy
    Sheets("Master Sheet").Range("B" & Sheets("Master Sheet").Cells(Rows.Count, 2).End(xlUp).Row).Offset(1).PasteSpecial -4123
    Sheets("Master Sheet").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    Application.CutCopyMode = False
    Rows("26:26").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Range("B12").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
    MsgBox "Order Sent"
End Sub
Thanks very much It does copy and paste every time, However it doesn't clear the table in the active sheet.
 
Upvote 0
Add this line
VBA Code:
   Range("Table34").ClearContents

Before this line
VBA Code:
  ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=""
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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