VBA Workbook Protection question

Emmily

Well-known Member
Joined
Oct 5, 2008
Messages
676
Hi, i have the following parameters for workbook protection. Now if i make this True

, AllowDeletingRows:=True _

then the user can delete all rows. I would like to limit to what they can delete.

So how can i add to this so that the user is able to delete rows for wks "Test1" and Test2" from range "C7:X1000" only?

Code:
Public Sub protectSheet(ByRef wks As Worksheet, ByVal strPW_Wks As String)

    wks.Protect _
        Password:=strPW_Wks _
        , DrawingObjects:=True _
        , Contents:=True _
        , Scenarios:=True _
        , UserInterfaceOnly:=False _
        , AllowFormattingCells:=False _
        , AllowFormattingColumns:=True _
        , AllowFormattingRows:=True _
        , AllowInsertingColumns:=False _
        , AllowInsertingRows:=False _
        , AllowInsertingHyperlinks:=False _
        , AllowDeletingColumns:=False _
        , AllowDeletingRows:=True _
        , AllowSorting:=True _
        , AllowFiltering:=True _
        , AllowUsingPivotTables:=False
    
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Sorry. I was wrong. You can set the locked pproperty to false for a given range. Then that range will not be locked.

Code:
Sub protects()

Range("B9:F30").Locked = False
ActiveSheet.protect Contents:=True
End Sub
 
Upvote 0
Why doesn't this work?

Code:
Sub protects()

With Sheets(1)
    With .Range("A1:X3")
        .Locked = False
         .Unprotect Password:="TEST", AllowDeletingRows:=True
    End With
   
End With
End Sub
 
Upvote 0
Ok, i have this code now, but how can restrict the user from deleting rows from A1:X4, but still allow deleting rows from rows 5 onwards.

Code:
Sub Test()

Dim pw As String
Dim lRowFrom As Long
Dim lRowTo As Long

lRowFrom = 1
lRowTo = 4

    pw = "TEST"
    With ActiveSheet
        .Unprotect pw
        .Cells.Locked = False
        .Range("A" & lRowFrom & ":X" & lRowTo).Cells.Locked = True
        .Protect pw, _
            DrawingObjects:=False, _
            Contents:=True, _
            Scenarios:=False, _
            AllowFormattingCells:=True, _
            AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, _
            AllowInsertingColumns:=True, _
            AllowInsertingRows:=True, _
            AllowDeletingColumns:=True, _
            AllowDeletingRows:=True, _
            AllowSorting:=True, _
            AllowFiltering:=True, _
            AllowUsingPivotTables:=True
    End With
End Sub
 
Upvote 0
how can restrict the user from deleting rows from A1:X4

You are refering to it as a Range. Try refering to Rows

Rich (BB code):
 With ActiveSheet
        .Unprotect pw
        .Cells.Locked = False
        .Rows("1:4").Cells.Locked = True
 
Upvote 0
You are refering to it as a Range. Try refering to Rows

Rich (BB code):
 With ActiveSheet
        .Unprotect pw
        .Cells.Locked = False
        .Rows("1:4").Cells.Locked = True

Nope i tried that, but still if i select rows 1:4 it still allows me to delete the rows.
 
Upvote 0
With my test worksheet, and your code (modified) it works for me.


Rich (BB code):
Sub Test()
Dim pw As String
    pw = "TEST"
    With ActiveSheet
        .Unprotect pw
        .Cells.Locked = False
        .Rows("1:4").Cells.Locked = True
       
        .Protect pw, _
            DrawingObjects:=False, _
            Contents:=True, _
            Scenarios:=False, _
            AllowFormattingCells:=True, _
            AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, _
            AllowInsertingColumns:=True, _
            AllowInsertingRows:=True, _
            AllowDeletingColumns:=True, _
            AllowDeletingRows:=True, _
            AllowSorting:=True, _
            AllowFiltering:=True, _
            AllowUsingPivotTables:=True
    End With
End Sub
 
Upvote 0
So when you right click rows 1:4 delete is shaded grey, but from rows 5 onwards the delete is active.

With my test worksheet, and your code (modified) it works for me.


Rich (BB code):
Sub Test()
Dim pw As String
    pw = "TEST"
    With ActiveSheet
        .Unprotect pw
        .Cells.Locked = False
        .Rows("1:4").Cells.Locked = True
       
        .Protect pw, _
            DrawingObjects:=False, _
            Contents:=True, _
            Scenarios:=False, _
            AllowFormattingCells:=True, _
            AllowFormattingColumns:=True, _
            AllowFormattingRows:=True, _
            AllowInsertingColumns:=True, _
            AllowInsertingRows:=True, _
            AllowDeletingColumns:=True, _
            AllowDeletingRows:=True, _
            AllowSorting:=True, _
            AllowFiltering:=True, _
            AllowUsingPivotTables:=True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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