Vba 阻止用户根据某行中某个单元格的内容删除该行

Vba 阻止用户根据某行中某个单元格的内容删除该行,vba,excel,Vba,Excel,我有一个要保护的模板文件,这样用户就不能修改公式了。由于工作表受到保护,我编写了一个宏,允许用户插入行。我还需要一个宏来允许用户删除行,但我要防止用户删除某些关键行(例如,检查总计和标题等) 为此,我在模板中使用了L列来标识不能删除的行。对于这些行,我在L列的那一行中有一个单词“keep”。我在下面写了一个基本的delete宏,但我需要修改它以查看所选范围的L列rRange和Exit Sub,如果有单词“keep” *请注意,rRange可能包含多个相邻行,因此如果这些行中的任何一行未通过测试,

我有一个要保护的模板文件,这样用户就不能修改公式了。由于工作表受到保护,我编写了一个宏,允许用户插入行。我还需要一个宏来允许用户删除行,但我要防止用户删除某些关键行(例如,检查总计和标题等)

为此,我在模板中使用了L列来标识不能删除的行。对于这些行,我在L列的那一行中有一个单词
“keep”
。我在下面写了一个基本的delete宏,但我需要修改它以查看所选范围的L列
rRange
Exit Sub
,如果有单词
“keep”

*请注意,
rRange
可能包含多个相邻行,因此如果这些行中的任何一行未通过测试,宏将需要退出

Sub DeteteRows()

Dim rRange As Range
On Error Resume Next
    Application.DisplayAlerts = False
     Set rRange = Application.InputBox(Prompt:= _
            "Please use mouse to select a row to Delete.", _
                Title:="SPECIFY ROW TO DELETE", Type:=8)
On Error GoTo 0
    Application.DisplayAlerts = True
    If rRange Is Nothing Then

    Exit Sub

    Else

rRange.EntireRow.Delete
Range("a1").Select

MsgBox ("Row(s) Deteted")
    End If

End Sub

这可能不是最好的方法,但它是下面的。我没有在最后一个if-then-else中添加delete部分,因为我认为您可以处理它

Sub DeteteRows()
Dim rRange As Range
Dim bKeepFound As Boolean
bKeepFound = False
On Error Resume Next
Application.DisplayAlerts = False
Set rRange = Application.InputBox(Prompt:= _
"Please use mouse to select a row to Delete.", _
Title:="SPECIFY ROW TO DELETE", Type:=8)
On Error GoTo 0
    Application.DisplayAlerts = True
    If rRange Is Nothing Then
        Exit Sub
        'dont need the else statement cause you exit the sub if it fails
    End If

    For Each Row In rRange.Rows
    Dim s 'variable to hold the array
    s = Split(Row.Address, ":") 'split out the column and row
        'remove the $ and convert to a number then check the cell value
        If rRange.Cells(CInt(Replace(s(0), "$", "")), 12).Value = "keep" Then
            bKeepFound = True
        End If
    Next Row
    'check to see if a row was found to keep
    If bKeepFound Then
        Exit Sub 'row was found so exit sub
    Else
        'delete the rows in the range
    End If

End Sub