使用.Protection.AllowEditRanges.Add解锁多于一列

时间:2018-07-27 08:47:27

标签: excel vba excel-vba

嘿,我有一个无法解决的小问题...所以我真的可以使用一些帮助! 我试图在我的代码中解锁4列,但我只能解锁3 ...如果我添加第四列,则会出现错误“对象'_global'的方法'范围'失败”

Sub UnLockStatusSheet()
    StatusSheet.Cells(1, 1).Select

    ' Allow Software column header
    Dim SoftwareRange As Range
    Set SoftwareRange = FindAllColumnHeaderIndex(2, "Planned to be implemented")
    For Each Cell In SoftwareRange
        StatusSheet.Protection.AllowEditRanges.Add Title:="Software" & Cell.Column, _
                Range:=Range(Cell.Offset(-1, 0).Address)
    Next Cell

    ' Protect sheet except applicable rows
    Dim AllowedRange As Range
    Set AllowedRange = StatusSheet.Range("E2", "E" & LastRowInOneColumn(1))

    For Each Line In AllowedRange.Rows
        If (Line.Value = "Applicable") Then
            StatusSheet.Protection.AllowEditRanges.Add Title:="AllowedRange" & Line.Row, _
                Range:=Range("M" & Line.Row & ":AX" & Line.Row)
            StatusSheet.Protection.AllowEditRanges.Add Title:="AllowedRangeUser" & Line.Row, _
                Range:=Range("D" & Line.Row) '<<<<< my problem is here!!! 

        End If
    Next Line

    ' Enable autofiltering
    StatusSheet.Rows("2:2").AutoFilter

    ' Lock sheet
    StatusSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowSorting:=True, AllowFiltering:=True, AllowFormattingColumns:=True _
        , AllowFormattingCells:=True

    Application.ScreenUpdating = True
End Sub

我想在& "E" & Line.Row之后添加"D" & Line.Row,所以我不仅解锁了D列和E列,而且还解锁了D列。但是我可以得到错误...如果没有& "E" & Line.Row代码工作正常,我不明白为什么...对此问题有解释吗?谢谢

0 个答案:

没有答案