Leonvl
New Member
- Joined
- Apr 26, 2016
- Messages
- 20
I want to run a script when leaving a worksheet.
When I put the following code the code runs in the current sheet and not in the targeted sheets. What's wrong...?
When I put the following code the code runs in the current sheet and not in the targeted sheets. What's wrong...?
Code:
Sub Workbook_Deactivate()
Dim ws As Worksheet
Dim x As Long
Application.ScreenUpdating = False
Application.enableEvents = False
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "CF*" Or ws.Name Like "Scenario*" Then
ws.Activate
Application.Run "HideColX"
Application.Run "HideRowX"
End If
Next ws
Application.enableEvents = True
Application.ScreenUpdating = True
End Sub
Private Sub HideColX()
Dim x As Long
For x = 3 To 47
If UCase(Cells(1, x).Value) = "X" Then
Columns(x).Hidden = True
Else
Columns(x).Hidden = False
End If
Next
End Sub
Private Sub HideRowX()
Dim x As Long
For x = 5 To 47
If UCase(Cells(x, 1).Value) = "X" Then
Rows(x).Hidden = True
Else
Rows(x).Hidden = False
End If
Next
End Sub