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

时间:2011-10-14 15:08:26

标签: excel vba excel-vba

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

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

*请注意,"keep"可能包含许多相邻的行,因此如果其中任何行未通过测试,则需要退出宏。

rRange

1 个答案:

答案 0 :(得分:0)

这可能不是最好的方式,但它在下面。我没有在最后一个中添加删除部分,因为我认为你可以处理

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