How do I get the Source sheet to become password protected after using an active x button to make a copy of the active sheet?

DMatt

New Member
Joined
May 12, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
The code below is activated from an active x button. The code will unprotect the active workbook and worksheet, make a copy of the active worksheet, place that new sheet's tab where I want it to be placed, protect the new worksheet, and protect the workbook. It fails to protect the Source Worksheet that the copy was made from. I need the code to password protect the Last Active Sheet too (the Source Sheet the Copy was made from). Any help is greatly appreciated.

Private Sub CommandButton13_Click()
ThisWorkbook.Unprotect Password:="Password1"
ActiveSheet.Unprotect Password:="Password2"
Dim i As Long, s As String, s2 As String
s = "NEW WONDERFUL WORKSHEET"
s2 = s
ActiveSheet.Copy After:=Sheets(2)
Do While WorksheetExists(s2)
i = i + 1
s2 = s & " " & i
Loop
ActiveSheet.Name = s2
ActiveSheet.Protect Password:="Password2"
ThisWorkbook.Protect Password:="Password1"
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Maybe something like this.

VBA Code:
Private Sub CommandButton13_Click()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim i As Long, s As String, s2 As String

    Set WS1 = ActiveSheet

    ThisWorkbook.Unprotect Password:="Password1"
    WS1.Unprotect Password:="Password2"

    s = "NEW WONDERFUL WORKSHEET"
    s2 = s

    WS1.Copy After:=Sheets(2)
    Set WS2 = ActiveSheet

    Do While WorksheetExists(s2)
        i = i + 1
        s2 = s & " " & i
    Loop

    WS2.Name = s2
    WS2.Protect Password:="Password2"
    WS1.Protect Password:="Password2"
    ThisWorkbook.Protect Password:="Password1"
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above
it makes the code easier to read.)
 
Upvote 0
Solution
Thank you rlv01, that did what I needed it to do.
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

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