保护第一行不被删除

我有一个带有命令button的受保护的Excel表格来删除行。 用户可以突出显示并select多行并通过单击命令button将其删除。 但我不希望第一行(第11行)被删除,因为它包含公式和格式。 我有下面的代码在命令button来删除。 它工作正常,并保护第11行,当用户突出显示和select第11行到第40行(因为这里的活动行是11)。 但是问题出在用户高亮显示表单button的行并点击命令button的时候,它甚至会删除第11行,因为这里的活动行大于11(例如活动行= 40)。 请尽量保护第11行不被用户删除,即使他们select删除。

Private Sub CommandButton2_Click() If ActiveCell.Row > 11 Then ActiveSheet.Unprotect "xxx" Selection.EntireRow.Delete ActiveSheet.Protect "xxx", True, True End If End Sub 

这是一种方法。 检查用户是否select了第11行

 Private Sub CommandButton2_Click() Dim rng As Range Dim rw As Variant '~~> Check if what the user selected is a valid range If TypeName(Selection) <> "Range" Then MsgBox "Select a range first." Exit Sub End If Set rng = Selection '~~> Check if the user has selected Row 11 For Each rw In rng.Rows If rw.Row = 11 Then MsgBox "Please do not select Row 11" Exit Sub End If Next rw ActiveSheet.Unprotect "xxx" rng.Rows.Delete ActiveSheet.Protect "xxx", True, True End Sub