2015-10-22 173 views
1

昨天我問了一個問題(Marking an area with a double 'For' loop),@Excel Hero給了我一個很好的迴應。我用自己的代碼來存儲範圍變量「K」:VBA:Protection.AllowEditRanges動態範圍

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

這已經是一個範圍,所以你不想換K的一個範圍()方法:

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