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
 
Do you have any other code in that sheet module?
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
There is code in another module that runs a macro to 'sort' the sheet in a particular order. I removed this but it did not help.

I created a new excel workbook and entered the code and it worked as expected, so it look like there is something in my original workbook that prevents it from working correctly.

Thanks Fluff for your assistance the code you provided works correctly. Looks like I will have to create the workbook and data from scratch.

Thanks again Fluff
 
Upvote 0

Forum statistics

Threads
1,223,712
Messages
6,174,031
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