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

时间:2017-09-29 08:48:38

标签: excel vba excel-vba

当我按下另一个工作表时,deactivate事件中的代码可以正常工作。唯一不起作用的是程序结束前的最后2行(保护工作表)。我有另一个停用事件过程,也保护相同的2个工作表,它的工作原理。它只适用于这个特定的一个。我也尝试过工作表(2).Protect和Worksheets(4).Protect,它们也不能用于去激活事件。

当我使用F8进行调试时,它会逐行运行代码,它将保护工作表并按预期工作。但它不会通过停用事件激活。

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图像打开了工作簿和宏:

enter image description here

另一个事件的代码,它是同一工作表的deactivate事件之前的worksheet_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

0 个答案:

没有答案