检测整个表中的任何(整个)行select并取消保护?
寻找下面的代码:
工作表当前被locking(启用locking的单元格select)。
VBA检测是否有整行(21,22)被选中,并自动取消保护表单。
然后:
如果这些确切的行被删除..表自动保护再次。
如果用户取消select这些行,表单将再次保护。
(这是devise来执行特定的行删除)
非常粗暴地说:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) IF Rows("1:1").Select AND/OR Rows("2:2").Select AND/OR Rows("3:3").Select then ActiveSheet.Unprotect End If ActiveCell.Row.Delete ActiveSheet.Protect End Sub
记得先设置Application.enableEvents = True
编辑更改代码作为讨论中的OP的新规范
限制:ENTIRE行(每个单元格必须解锁以便能够select整行)
' remember the event's name is `Worksheet_SelectionChange` ' NOT Worksheet1_SelectionChange Private Sub Worksheet_SelectionChange(ByVal Target As Range) ActiveSheet.Unprotect ' the rows to be selected Dim row1 As Range Dim row2 As Range Dim row3 As Range Dim mergedRange As Range Set row1 = Me.Rows("1:1") Set row2 = Me.Rows("3:3") Set row3 = Me.Rows("5:5") Dim found As Boolean Dim Match As Boolean Set mergedRange = Application.Union(row1, row2) Set mergedRange = Application.Union(mergedRange, row3) Match = False ' check if it selects only 1 row If Target.Areas.Count <> 1 Then ActiveSheet.Protect Exit Sub End If ' check if it's select the first 500 rows If Target.Areas.Item(1).Row > 0 And Target.Areas.Item(1).Row <= 500 Then 'check if it's selecting the WHOLE row If Me.Rows(Target.Areas.Item(1).Row & ":" & Target.Areas.Item(1).Row).Areas.Item(1).Count = Target.Areas.Item(1).Count Then ' check if the "B" Column of this row's backgound color is blue If Me.Cells(Target.Areas.Item(1).Row, 2).Interior.Color = RGB(197, 217, 241) Then Match = True End If End If End If If Match Then 'MsgBox "ActiveSheet.Unprotect" ActiveSheet.Unprotect Else Debug.Print "notMatch" 'ActiveCell.Row.Delete ActiveSheet.Protect End If End Sub