vba - 清除未 protected 单元格的按钮

标签 vba excel

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

最佳答案

工作表是 protected , 单元格是 锁定 .

试试这个:

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

编辑#1:这将清除 内容 只要:
Sub ClearStuff()
    Dim r As Range, rClear As Range
    Set rClear = Nothing
    For Each r In ActiveSheet.UsedRange
        If r.Locked = False Then
            If rClear Is Nothing Then
                Set rClear = r
            Else
                Set rClear = Union(rClear, r)
            End If
        End If
    Next r
    rClear.ClearContents
End Sub

关于vba - 清除未 protected 单元格的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286070/

相关文章:

excel - 另存为并禁用公式和宏

vba - 检查新收到的电子邮件是否有附件

excel - 希望返回给定公司名称的行的最大值

.net - 如何将 Excel 日期序列号转换为 .NET DateTime?

excel - 禁用按钮,直到按下另一个按钮,如果单元格值更改,则再次禁用它

excel - VBA WinHTTP从受密码保护的https网站下载文件

excel - 更改代码以允许选择多个文件

excel - 在 vba 中缩进 mpp 文件(Ms 项目文件)中的任务(excel 到 mpp 文件)

检查工作簿是否存在的 VBA 函数

c# - 使用互操作在后台打开 Excel 工作簿