从 VB 脚本保护中排除列

Excluding a column from VB script protection

我是 VB 脚本的新手,对它的工作原理知之甚少。

使用以下代码的想法是,用户将能够编辑/向 Excel 作品sheet 添加详细信息,但如果没有密码则无法删除任何数据。其中一列用于添加评论,并不强制要求每个项目都必须始终有评论。下面的代码将盲目地保护整个 sheet 但我需要在列 N6:N1000 中添加一个排除项。请这里的人帮我添加这个排除项。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim sPassCheck As String

    Dim rng As Range

    Dim sTemp As String

    Dim sPassword As String


    sPassword = "12345"

    sTemp = "You must enter the password to delete data"


    'Use to set a single cell if more than one cell is

    'in the target range

    If Target.Count > 1 Then

        Set rng = Target.Cells(1, 1)

    Else

        Set rng = Target

    End If


    If rng.Value = "" Then

        sPassCheck = InputBox(sTemp, "Delete check!")

        Application.EnableEvents = False

        If sPassCheck <> sPassword Then Application.Undo

    End If


    Application.EnableEvents = True

End Sub

这个修改就可以了

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim sPassCheck As String
    Dim rng As Range
    Dim sTemp As String
    Dim sPassword As String

    sPassword = "12345"
    sTemp = "You must enter the password to delete data"

    ' Check if target is within Range N6:N1000
    If Intersect(Target, Range("N6:N1000")) Is Nothing Then

        If Target.Count > 1 Then
            Set rng = Target.Cells(1, 1)
        Else
            Set rng = Target
        End If


        If rng.Value = "" Then

            sPassCheck = InputBox(sTemp, "Delete check!")

            Application.EnableEvents = False

            If sPassCheck <> sPassword Then Application.Undo

        End If
    End If

    Application.EnableEvents = True

End Sub