• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

Event macro unprotects worksheet, then changes protection parameters - not what I want.

I use the following code to unprotect a worksheet and delete a row, retaining formulas and formats, but deleting constants, then turning protection on again..

Works great.

The problem is that I want to allow users of the protected worksheet to 'Select Unlocked Cells,' 'Select Locked Cells,' 'Format Cells,' but when the macro turns protection back on, the ability to format goes away, and all they can do is Select the unlocked and locked cells.

How can I lock in my protection parameters? (Disclaimer - I know very little, and have gotten this far by getting help here and by searching the web.)

Thanks!

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  'David McRitchie,  2007-09-07    insrtrow.htm on double-click
  '-- will copy more often than  Extend Formulas and Format (tools option)
  Cancel = True
  If Target.Row < 24 Then Exit Sub
  Sheet1.Unprotect Password:="Encore"
  Target.EntireRow.Copy
    Cells(Target.Row + 1, 1).EntireRow.Insert
  Cells(Target.Row + 1, 1).EntireRow.Select
  ActiveSheet.Paste
  Application.CutCopyMode = False
  On Error Resume Next
  '-- customize range for what cells constants can be removed --
  Intersect(Selection, Range("a:IV")).SpecialCells(xlConstants).ClearContents
  ActiveCell.Offset(0, 2).Select
  Sheet1.Protect Password:="Encore"
  On Error GoTo 0
 
End Sub
0
eracer
Asked:
eracer
1 Solution
 
Rgonzo1971Commented:
Hi,

pls try

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  'David McRitchie,  2007-09-07    insrtrow.htm on double-click
  '-- will copy more often than  Extend Formulas and Format (tools option)
  Cancel = True
  If Target.Row < 24 Then Exit Sub
  Sheet1.Unprotect Password:="Encore"
  Target.EntireRow.Copy
    Cells(Target.Row + 1, 1).EntireRow.Insert
  Cells(Target.Row + 1, 1).EntireRow.Select
  ActiveSheet.Paste
  Application.CutCopyMode = False
  On Error Resume Next
  '-- customize range for what cells constants can be removed --
  Intersect(Selection, Range("a:IV")).SpecialCells(xlConstants).ClearContents
  ActiveCell.Offset(0, 2).Select
  Sheet1.Protect Password:="Encore", AllowFormattingCells:=True ' the default is False 
  On Error GoTo 0
  
End Sub

Open in new window

Regards
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now