Run Macro on various other sheets

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...?

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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
The current sheet name is "Project Setup"
Your event code triggers when you deactivate the workbook it is in. Given that it's not active, you can't activate a sheet in it. Untested but see if this works:
Code:
Sub Workbook_Deactivate()
    Dim ws As Worksheet
    Dim x As Long
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    For Each ws In Me.Worksheets
      If ws.Name Like "CF*" Or ws.Name Like "Scenario*" Then
        Application.Run "HideColX"
        Application.Run "HideRowX"
      End If
      Next ws
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top