为什么Worksheets.Protect在此VBA Worksheet_Deactivate()事件中不起作用?

当我按另一个工作表时,停用事件中的代码完美工作。 唯一不起作用的是过程结束之前的最后两行(保护工作表)。 我有另一个停用事件过程,也保护相同的2个工作表,它的工作原理。 它只在这个特定的一个不起作用。 我也尝试了工作表(2).Protect和工作表(4).Protect,他们也不工作在停用事件。

当我使用F8进行debugging时,它将逐行运行代码,它将保护工作表并按照原样工作。 但是它不会通过停用事件激活。

Private Sub Worksheet_Deactivate() Dim Total_rows_Fin As Long Dim Total_rows_Dash As Long Dim i As Long Dim j As Long Dim z As Long Total_rows_Dash = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Dashboard").Range("A" & Rows.count).End(xlUp).Row Total_rows_Fin = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Finished").Range("A" & Rows.count).End(xlUp).Row j = 0 ReDim c(0 To Total_rows_Dash) As Long For i = 2 To Total_rows_Dash If Worksheets("Dashboard").Cells(i, 6) = "D" Then j = j + 1 For z = 1 To 6 Worksheets("Finished").Cells(Total_rows_Fin + j, z) = Worksheets("Dashboard").Cells(i, z) c(i) = 1 Next z End If Next i For i = 2 To Total_rows_Dash For j = 2 To 6 If c(i) = 1 Then Worksheets("Dashboard").Cells(i, j).Delete Shift:=xlUp Next j Next i Worksheets("Prod. Qty.").Protect Worksheets("Dashboard").Protect End Sub 

以下是一些额外的信息:

我的VBE的图像打开工作簿和macros:

在这里输入图像说明

在同一个工作表的停用事件之前的另一个事件代码,即workheet_activate事件:

 Option Explicit Private Sub Worksheet_Activate() Dim Total_rows_Pick As Long Dim Total_rows_Dash As Long Dim Total_rows_Prod As Long Dim Total_rows_Fin As Long Dim Total_rows_PC As Long Dim i As Long Dim j As Long Dim k As Long Dim c As Long Dim y As Long Dim m As Variant Worksheets("Prod. Qty.").Unprotect Worksheets("Dashboard").Unprotect 'Clears Contents Total_rows_Dash = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Dashboard").Range("A" & Rows.count).End(xlUp).Row Worksheets("Dashboard").Range("A2:A1048576").ClearContents Worksheets("Dashboard").Range("B2:B1048576").ClearContents Worksheets("Dashboard").Range("C2:C1048576").ClearContents Worksheets("Dashboard").Range("D2:D1048576").ClearContents Worksheets("Dashboard").Range("E2:E1048576").ClearContents 'Counts total number of rows c = 0 Total_rows_Pick = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Pick-ups").Range("B" & Rows.count).End(xlUp).Row Total_rows_Prod = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Input").Range("A" & Rows.count).End(xlUp).Row Total_rows_Fin = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Finished").Range("A" & Rows.count).End(xlUp).Row Total_rows_Dash = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Dashboard").Range("A" & Rows.count).End(xlUp).Row For i = 2 To Total_rows_Pick m = 0 c = 0 For j = 1 To Total_rows_Dash If Worksheets("Pick-ups").Cells(i, 2) = Worksheets("Dashboard").Cells(j, 2) And Worksheets("Pick-ups").Cells(i, 4) = Worksheets("Dashboard").Cells(j, 1) Then c = c + 1 End If Next j For y = 1 To Total_rows_Fin If c = 0 And Worksheets("Finished").Cells(y, 2) = Worksheets("Pick-ups").Cells(i, 2) And Worksheets("Finished").Cells(y, 1) = Worksheets("Pick-ups").Cells(i, 4) Then m = 1 End If Next y If m = 0 Then Worksheets("Dashboard").Cells(Total_rows_Dash + 1, 2) = Worksheets("Pick-ups").Cells(i, 2) Worksheets("Dashboard").Cells(Total_rows_Dash + 1, 1) = Worksheets("Pick-ups").Cells(i, 4) Total_rows_Dash = Workbooks("Hourly Production Monitoring.xlsm").Worksheets("Dashboard").Range("A" & Rows.count).End(xlUp).Row End If Next i For i = 2 To Total_rows_Pick For j = 2 To Total_rows_Dash m = Application.Match(Worksheets("Pick-ups").Cells(i, 4), Worksheets("Finished").Range("A2:A1048576"), 0) If IsError(m) Then If Worksheets("Dashboard").Cells(j, 2) = Worksheets("Pick-ups").Cells(i, 2) And Worksheets("Pick-ups").Cells(i, 4) = Worksheets("Dashboard").Cells(j, 1) Then Worksheets("Dashboard").Cells(j, 3) = Worksheets("Dashboard").Cells(j, 3) + Worksheets("Pick-ups").Cells(i, 3) End If End If Next j Next i For k = 2 To Total_rows_Prod For j = 2 To Total_rows_Dash m = Application.Match(Worksheets("Prod. Qty.").Cells(k, 5), Worksheets("Finished").Range("A2:A1048576"), 0) If IsError(m) Then If Worksheets("Dashboard").Cells(j, 2) = Worksheets("Prod. Qty.").Cells(k, 3) And Worksheets("Prod. Qty.").Cells(k, 5) = Worksheets("Dashboard").Cells(j, 1) Then Worksheets("Dashboard").Cells(j, 4) = Worksheets("Dashboard").Cells(j, 4) + Worksheets("Prod. Qty.").Cells(k, 18) End If End If Next j Next k Total_rows_PC = Worksheets("Dashboard").Range("E" & Rows.count).End(xlUp).Row If Total_rows_PC <> Total_rows_Dash And Total_rows_PC <= Total_rows_Dash Then If Total_rows_PC = 1 Then Worksheets("Dashboard").Cells(2, 5) = "=(D2/C2)*100" End If End If Total_rows_PC = Worksheets("Dashboard").Range("E" & Rows.count).End(xlUp).Row If Total_rows_PC <> Total_rows_Dash And Total_rows_PC <= Total_rows_Dash Then Range("E" & Total_rows_PC).AutoFill Destination:=Range("E" & Total_rows_PC, "E" & Total_rows_Dash), Type:=xlFillDefault End If End Sub