取消选择子范围

时间:2015-05-05 14:58:49

标签: vba

我想在工作表中锁定范围A4:B9。现在正如looks like那样,实现这一目标的唯一方法是将Lock=false状态设置为我不想被锁定的所有单元格,然后保护整个文件。

我的问题是,我只有一小部分需要保护,所以我需要找到一种方法来创建一个包含所有单元格的范围,例如A4:B9。我知道交叉和联合,但不能想出应用它们来实现我的目标。

1 个答案:

答案 0 :(得分:0)

我写的这对我有用:

Sub deselect_subranges()
Dim cell As Range
Dim rngAll As Range
Dim rngMy As Range
Dim rngNew As Range

Set rngAll = ThisWorkbook.Worksheets(1).Range("A1:AZ400")
Set rngMy = ThisWorkbook.Worksheets(1).Range("C3:E8")

Set rngNew = Nothing

For Each cell In rngAll
    If Intersect(cell, rngMy) Is Nothing Then
        If rngNew Is Nothing Then
            Set rngNew = cell
        Else
            Set rngNew = Union(rngNew, cell)
        End If
    End If
Next cell

rngNew.Select

End Sub