将 Excel VBA 脚本隔离到 运行 特定工作表?

Isolate Excel VBA script to run aginst specific worksheets?

我花了几个小时尝试不同的修改,但其中一定没有奏效。 VBA 调试器不会抛出任何错误,当我测试脚本时,它从未出现 运行.

每当我从任何工作sheet 选项卡保存文档时,如何针对特定工作sheet 将下面的脚本修改为运行?

谢谢


VBA - 锁定单元格并在保存时保护 Sheet

下面的脚本将锁定包含值的单元格,然后在保存前用密码保护 sheet。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    On Error Resume Next
    Dim Cell As Range
    With ActiveSheet
        .Unprotect Password:=""
        .Cells.Locked = False
        For Each Cell In Application.ActiveSheet.UsedRange
            If Cell.Value = "" Then
                Cell.Locked = False
            Else
                Cell.Locked = True
            End If
        Next Cell
        .Protect Password:=""
         'Protect with blank password, you can change it
    End With
    Exit Sub
End Sub

Script Source

更改 ActiveSheet 并使用 For Each 循环,如下所示:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    On Error Resume Next
    Dim Cell As Range
    For Each sh In Array("Sheet1", "AnotherSheet", "OtherSheet")   
        With Sheets(sh)
            .Unprotect Password:=""
            .Cells.Locked = False
                For Each Cell In Application.ActiveSheet.UsedRange
                    If Cell.Value = "" Then
                        Cell.Locked = False
                    Else
                        Cell.Locked = True
                    End If
                Next
            .Protect Password:=""
        End With
    Next
End Sub

这应该对您有所帮助(您将收到消息,让您知道您何时参加活动以及活动何时开始和结束):

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Cell As Range
MsgBox "Event Workbook_BeforeSave Launched", vbInformation + vbOKOnly, "Started"

On Error GoTo ErrHandler

ReTry:
    With Sheet6
        .Unprotect Password:=""
        .Cells.Locked = False
        For Each Cell In .UsedRange
            If Cell.Value = "" Then
                Cell.Locked = False
            Else
                Cell.Locked = True
            End If
        Next Cell
        .Protect Password:=""
        'Protect with blank password, you can change it
    End With
    With Sheet7
        .Unprotect Password:=""
        .Cells.Locked = False
        For Each Cell In .UsedRange
            If Cell.Value = "" Then
                Cell.Locked = False
            Else
                Cell.Locked = True
            End If
        Next Cell
        .Protect Password:=""
        'Protect with blank password, you can change it
    End With

MsgBox "Event Workbook_BeforeSave Over", vbInformation + vbOKOnly, "Finished"
    Exit Sub
ErrHandler:
    MsgBox "Error " & Err.Number & " :" & vbCrLf & _
        Err.Description
    Resume ReTry

End Sub

代码可以通过

显着缩短(运行 时间)
  • 使用 SpecialCells 而不是遍历每个单元格
  • 避免将空白单元格设置为两次锁定(与第一点相比较小)。

已更新

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

For Each sh In Array("Sheet1", "AnotherSheet", "OtherSheet")
        With Sheets(sh)
            .Unprotect
            .Cells.Locked = True
                On Error Resume Next
                .Cells.SpecialCells(xlBlanks).Locked = False
                On Error GoTo 0
            .Protect
        End With
    Next
End Sub