用于清除未受保护的单元格的按钮

时间:2017-07-24 16:54:06

标签: excel vba excel-vba

我是VBA的新手。我正在尝试创建一个按钮来清除未受保护的单元格中的值。我有一张受保护的表格,但我想确保只清除未受保护的单元格。我需要一个单元格范围吗?提前致谢

1 个答案:

答案 0 :(得分:3)

工作表受保护,单元格已锁定

尝试一下:

Sub KlearStuff()
    Dim r As Range, rKlear As Range
    Set rclear = Nothing
    For Each r In ActiveSheet.UsedRange
        If r.Locked = False Then
            If rKlear Is Nothing Then
                Set rKlear = r
            Else
                Set rKlear = Union(rKlear, r)
            End If
        End If
    Next r
    rKlear.Clear
End Sub

编辑#1:
这将仅清除内容

Sub KlearStuff()
    Dim r As Range, rKlear As Range
    Set rclear = Nothing
    For Each r In ActiveSheet.UsedRange
        If r.Locked = False Then
            If rKlear Is Nothing Then
                Set rKlear = r
            Else
                Set rKlear = Union(rKlear, r)
            End If
        End If
    Next r
    rKlear.ClearContents
End Sub