sparky2205
Well-known Member
- Joined
- Feb 6, 2013
- Messages
- 507
- Office Version
- 365
- 2016
- Platform
- Windows
Hi folks,
can anyone tell me why this works every alternate time it's run?
Run it and it works fine.
Run it again "Paste method of Worksheet class failed"
Run it again it works fine.
Run it again "Paste method of Worksheet class failed"
etc.....
Thanks.
can anyone tell me why this works every alternate time it's run?
Run it and it works fine.
Run it again "Paste method of Worksheet class failed"
Run it again it works fine.
Run it again "Paste method of Worksheet class failed"
etc.....
Thanks.
Code:
Sub CopyRows()
Dim RowNum As Integer
Dim pw As String
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
pw = "Test"
Rows(LastRow & ":" & LastRow - 1).Activate
Selection.Copy
Rows(LastRow + 2).Select
ActiveSheet.Unprotect Password:=pw
Application.CutCopyMode = xlCopy
ActiveSheet.Paste
ActiveSheet.Range("B" & LastRow + 2 & ":" & "K" & LastRow + 2).ClearContents
ActiveSheet.Range("B" & LastRow + 1).Locked = False
ActiveSheet.Protect Password:=pw & _
AllowFormattingCells & _
AllowFormattingRows & _
AllowFormattingColumns
End Sub