如果我将流程拆分为3个单独的子流程,下面的代码可以正常工作,但我将这个问题合并到一个流程中。
理想情况下,我希望代码能够运行并临时解锁“工作表”,执行“Worksheet_Calculate”子工作,然后再次锁定“工作表”。
有人可以帮忙吗。
Private Sub UnprotectAll()
Dim sh As Worksheet
Dim yourPassword As String
yourPassword = "Test"
For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect Password:=yourPassword
Next sh
End Sub
Private Sub Worksheet_Calculate()
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row
On Error Resume Next
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row
On Error Resume Next
For Each Dependents In Range("G6")
For Each HideDependents In Range("G18:G19")
If Dependents.Value = 0 Then
HideDependents.EntireRow.Hidden = True
ElseIf Dependents.Value >= 1 Then
HideDependents.EntireRow.Hidden = False
End If
Next
Next
For Each Vehicle In Range("G7")
For Each HideVehicle In Range("G45:G48")
If Vehicle.Value = 0 Then
HideVehicle.EntireRow.Hidden = True
ElseIf Vehicle.Value >= 1 Then
HideVehicle.EntireRow.Hidden = False
End If
Next
Next
For Each Joint In Range("G9")
For Each HideJoint In Range("I14:J65")
If Joint.Value = 0 Then
HideJoint.EntireColumn.Hidden = True
ElseIf Joint.Value = 1 Then
HideJoint.EntireColumn.Hidden = False
End If
Next
Next
On Error GoTo 0
Application.EnableEvents = True
End Sub
Private Sub ProtectAll()
Dim sh As Worksheet
Dim yourPassword As String
yourPassword = "Test"
For Each sh In ActiveWorkbook.Worksheets
sh.Protect Password:=yourPassword
Next sh
End Sub
答案 0 :(得分:0)
还可以获得许多其他效率,但这是一种结合方式,将每个表格循环包裹在整个事物周围。
Private Sub Combine()
Dim sh As Worksheet
Dim yourPassword As String
yourPassword = "Test"
For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect Password:=yourPassword
Dim LastRow As Long, c As Range
Application.EnableEvents = False
LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row
On Error Resume Next
For Each Dependents In Range("G6")
For Each HideDependents In Range("G18:G19")
If Dependents.Value = 0 Then
HideDependents.EntireRow.Hidden = True
ElseIf Dependents.Value >= 1 Then
HideDependents.EntireRow.Hidden = False
End If
Next
Next
For Each Vehicle In Range("G7")
For Each HideVehicle In Range("G45:G48")
If Vehicle.Value = 0 Then
HideVehicle.EntireRow.Hidden = True
ElseIf Vehicle.Value >= 1 Then
HideVehicle.EntireRow.Hidden = False
End If
Next
Next
For Each Joint In Range("G9")
For Each HideJoint In Range("I14:J65")
If Joint.Value = 0 Then
HideJoint.EntireColumn.Hidden = True
ElseIf Joint.Value = 1 Then
HideJoint.EntireColumn.Hidden = False
End If
Next
Next
On Error GoTo 0
Application.EnableEvents = True
sh.Protect Password:=yourPassword
Next sh
End Sub