VBA:具有动态范围的Protection.AllowEditRanges

时间:2015-10-22 12:34:08

标签: vba range

昨天我问了一个问题(Marking an area with a double 'For' loop),@ Excel Hero给了我一个很好的回答。我使用他的代码在变量'k'中存储范围:

Sub ErosRam()
    Dim i&, j&, area$, k As Range, r As Range

    Const COL_PERIOD = 3
    Const ROW_PERIOD = 3
    Const REPS_HORIZONTAL = 3
    Const REPS_VERTICAL = 2

    Set r = [f7:g8]
    Set k = r

    For i = 0 To REPS_VERTICAL - 1
        For j = 0 To REPS_HORIZONTAL - 1
            Set k = Union(k, r.Offset(i * ROW_PERIOD, j * COL_PERIOD))
        Next
    Next

    area = k.Address(0, 0)
    MsgBox area   
End Sub

现在,我希望保持'k'给出的区域解锁,并锁定其余部分。所以我希望使用类似的东西:

     ActiveSheet.Protection.AllowEditRanges.Add Title:="test", Range:=Range(k)

但这不起作用。如何在protection.alloweditranges中使用变量范围?

1 个答案:

答案 0 :(得分:1)

它已经是一个范围,所以你不想用Range()方法包装k:

ActiveSheet.Protection.AllowEditRanges.Add Title:="test", Range:=k