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
 
jmacleary, Fluff

The Code is in a Worksheet "Sheet5(On Hire 01-01-2109)" and not a Module, does that make a difference?
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If that is the sheet you want it to work on, then that's fine.
Have you tried what I suggested in post#10?
 
Upvote 0
I tried the post #10 idea but it did not work, unless I am doing something stupid. What I have is the following code in my Sheet5(On Hire 01-01-2019)


Code:
Sub chk()
Application.EnableEvents = True
End Sub


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      
     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"


End Sub
 
Upvote 0
Is the code being executed?

You can check that by setting a breakpoint somewhere in the code using F9, then returning to the sheet and double clicking in a cell.

If the code is being triggered you should see a Debug dialog.
 
Upvote 0
I tried the post #10 idea but it did not work, unless I am doing something stupid. What I have is the following code in my Sheet5(On Hire 01-01-2019)
You need to run the sub chk manually, it's not automatic
 
Upvote 0
I have just noticed that if the Sheet is Unprotected to start with then everything works OK. It inserts a Row with all the Formulas and then Protects the sheet.
If the Sheet is Protected to start with then nothing happens at all
 
Upvote 0
That code should either work, or give you an error if the password is wrong.
 
Upvote 0
I am not getting any error messages. I just protected the sheet using the password 'QWE' and left the password in the code as '123' and it did not give me any errors either
 
Upvote 0
Is the code EXACTLY as shown in post#13?
Also are you double-clicking on sheet "On Hire 01-01-2109"?
 
Upvote 0
Exactly as post #13 . I just copied/paste it again from the post, but nothing is happening if the sheet is Protected to begin with.
And yes, double-clicking on sheet "On Hire 01-01-2019"?
 
Upvote 0

Forum statistics

Threads
1,223,712
Messages
6,174,033
Members
452,542
Latest member
Bricklin

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