Protect Sheet from inserting rows only in a specific range

旧街凉风 提交于 2019-12-11 01:46:25

问题


I am trying to find a way to allow the user to insert rows but only in a specific range when the sheet is protected. For example, I don't want the user to be able to insert a row between the rows "1" and "2" (because it will make my macros do funny things), but I want him to be able to insert a row everywhere else.

The following code allows the user to insert rows everywhere in the sheet, but that is not exactly what I want:

    ActiveSheet.Protect Password:="qwerty", DrawingObjects:=True, Contents:=True, Scenarios:=True _
    , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
    AllowFormattingRows:=True, AllowInsertingRows:=True, AllowDeletingRows:=True

Can somebody help me with this issue ?


回答1:


There's no way to prevent the user from inserting rows only in specific ranges, you could lock those cells and prevent selection of locked cells so the user doesn't select cells in the top two rows which might prevent them from accidentally doing it?

You could also lock row insertion and set UserInterfaceOnly:=True and deal with any row insertion through a macro, cancelling the procedure if they're trying to insert a row in the wrong place.

Is it a necessity that the users can insert rows throughout the worksheet?




回答2:


You can use the below code on Worksheet Selection Change event.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Row = 1 Or Target.Row = 2 Then
        'MsgBox "Check"
        ActiveSheet.Protect Password:="qwerty", DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True, AllowInsertingRows:=False, AllowDeletingRows:=True
    Else
        ActiveSheet.Unprotect Password:="qwerty"
    End If
End Sub


来源:https://stackoverflow.com/questions/45501247/protect-sheet-from-inserting-rows-only-in-a-specific-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!