防止用户根据该行中单元格的内容删除某些行

我有一个模板文件,我想要保护,以便用户不能修改公式。 在工作表被保护的时候,我写了一个macros来允许用户插入行。 我也想要一个macros允许用户删除行,但我想阻止用户删除某些关键的行(如检查总数和标题等)。

为此,我使用了模板中的列L来标识无法删除的行。 对于这些行,我在列L的那一行中有"keep"一词。我已经在下面写了一个基本的删除macros,但是我需要修改它以查看所选范围rRange列L并Exit Sub如果单词"keep"在那儿。

*请注意, rRange可能包含许多相邻的行,所以如果这些行中的任何一行testing失败,macros需要退出。

 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 

这可能不是最好的方法,但是它在下面。 我没有添加删除部分在最后如果然后其他,因为我觉得你可以处理

 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