Unprotect Sht, Run VBA , Protect Sheet

DctheDC

New Member
Joined
Jun 7, 2016
Messages
37
I am using the following VBA to copy a row complete with all the Formulas, but my users keep *accidentally* deleting the formulas so I want to protect Col B:K

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      
        
    Cancel = True
    Target.Offset(1).EntireRow.Insert
    Target.EntireRow.Copy Target.Offset(1).EntireRow
    On Error Resume Next
    Target.Offset(1).EntireRow.SpecialCells(xlConstants).ClearContents


End Sub

I cannot figure out how to Unprotected the Sheet, run the code above to copy the Row, and then Protect the sheet again.

I have been playing with ActiveSheet.Unprotect Password:="123" and then ActiveSheet.Protect Password:="123"but can't get it right


Any ideas?

Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      
    Me.Unprotect "123"
    Cancel = True
    Target.Offset(1).EntireRow.Insert
    Target.EntireRow.Copy Target.Offset(1).EntireRow
    On Error Resume Next
    Target.Offset(1).EntireRow.SpecialCells(xlConstants).ClearContents
    Me.Protect "123"

End Sub
 
Upvote 0
Try using Me, which is a reference to the worksheet the code module 'belongs' to.
Code:
    Cancel = True

    Me.Unprotect Password:="123"

    Target.Offset(1).EntireRow.Insert

    Target.EntireRow.Copy Target.Offset(1).EntireRow

    Target.Offset(1).EntireRow.SpecialCells(xlConstants).ClearContents

    Me.Protect Password:="123"
 
Upvote 0
It did nothing, it did not appear to unprotect the sheet and did not copy the Row, and gave no error messages
 
Upvote 0
If you put the word Stop as the first line of code & doubleclick on the sheet what happens?
 
Upvote 0
Just a thought - do you have this code in the worksheet or in a module? It needs to be in the worksheet where you are expecting it to work.
 
Upvote 0
If the code is in the correct sheet module.
Run this
Code:
Sub chk()
Application.EnableEvents = True
End Sub
and then try double-clicking on the relevant sheet
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,687
Members
452,994
Latest member
Janick

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