VBA range

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,362
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a button on my spreadsheet that copies a worksheet and pastes it in a new workbook. This is my code:

Code:
Private Sub cmdCopySheet_Click()
Worksheets("home").Unprotect Password:="costings"
    Dim CopyMonth As String
        CopyMonth = Range("I13").Value
        
        Worksheets(CopyMonth).Activate
        Worksheets(CopyMonth).Columns("A:E").copy
        'Worksheets("home").Activate
        Workbooks.Add
        ActiveSheet.Paste
Worksheets("home").Protect Password:="costings"
End Sub

It was working before I added in the password protection. However, once I added it in, I started having all these problems. Is there a better way to do it?

I13 =CONCATENATE(F13," ",H13)
F13: has the month
H13: has the year

The sheet names are the format "mmmm yyyy"

I get the error subscript out of range and the protect line of code (the last line) is highlighted.
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I comment the line out,
Worksheets("home").Protect Password:="costings" and it works fine.
 
Upvote 0
I would suspect it errors out because at that point there is two sheets named home and the code dosent know which is which because you havent declared parent workbook.
 
Upvote 0
The "New" workbook will be the active workbook when you try to protect the sheet & as that doesn't have a sheet called "Home" you get the error. Try
Code:
ThisWorkbook.Worksheets("home").Protect Password:="costings"
 
Upvote 0
The "New" workbook will be the active workbook when you try to protect the sheet & as that doesn't have a sheet called "Home" you get the error. Try
Code:
ThisWorkbook.Worksheets("home").Protect Password:="costings"

I tried this and this is the code I have now.

Code:
Private Sub cmdCopySheet_Click()
Worksheets("home").Unprotect Password:="costings"
    Dim CopyMonth As String
        CopyMonth = Range("I13").Value
        
        Worksheets(CopyMonth).Activate
        Worksheets(CopyMonth).Columns("A:E").copy
        ThisWorkbook.Worksheets("home").Protect Password:="costings"
        'Worksheets("home").Activate
        Workbooks.Add
        ActiveSheet.Paste

End Sub

With that code I still get the error "paste method of worksheet class failed" and "ActiveSheet.Paste" is highlighted if I press debug.
 
Upvote 0
I googled it and found this code
Code:
    Dim xWs As Worksheet
    Dim Rng As Range
    Set Rng = Application.Selection
    Application.Workbooks.Add
    Set xWs = Application.ActiveSheet
    Rng.Copy Destination: = xWs.Range("A1")

How do I incorporate it into my current code?
 
Upvote 0
Try
Code:
Private Sub cmdCopySheet_Click()
   Dim CopyMonth As String
   Worksheets("home").Unprotect Password:="costings"
   CopyMonth = Worksheets("home").Range("I13").Value
   Workbooks.Add
   With ThisWorkbook.Worksheets(CopyMonth)
      .Columns("A:E").Copy Range("A1")
   End With
   ThisWorkbook.Worksheets("home").Protect Password:="costings"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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