2016-09-26 69 views
1

下面的代碼工作正常,如果我將進程拆分成3個獨立的子文件夾,但我有問題將其組合到一個進程中。Excel VBA - 在一個工作表中調用多個子設備

理想情況下,我希望代碼運行並臨時解鎖'Sheet',執行'Worksheet_Calculate'子,然後再次鎖定'Sheet'。

有人請協助。


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

有許多可能被獲得,但這是一個結合其他方式效率,環繞整個事情的每張紙環。

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 
+0

謝謝您的答覆,它的工作原理卻是存在的腳本自動完成整個過程運行時,「G6:G9」任何方式的變化,因爲我現在有,以便手動運行該命令,以使更改生效地點。 –

+0

將以下內容添加到要自動更新/監視更改的工作表。 私人小組Worksheet_SelectionChange(BYVAL目標作爲範圍) 如果沒有相交(目標,範圍(「G6:G9」))是沒有那麼 呼叫結合 結束如果 結束小組 –

+0

完美的作品,感謝你的幫助。 –

相關問題