How can i get my macro to run corretly when the worksheet deactivates

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

got a problem here.

I have 35 sheets all of which I need to run the same macro when I exit them.
but when I exit them they are no longer the active sheet so it all goes wrong.
i'm hoping there might be a way to change activesheet to something else and solve this any ideas?
below is the trigger and the macro

thanks

Tony

Code:
Private Sub Worksheet_Deactivate()
Call Employees
End Sub

Code:
Sub [LEFT][COLOR=#222222][FONT=Verdana]Employees[/FONT][/COLOR][/LEFT]()
TRow = 64
If ActiveSheet.Range("Y" & TRow) = "Acceptable" Then Exit Sub
LrowRD = Sheets("Employees").Cells(Rows.Count, "D").End(xlUp).Row + 1
If LrowRD < 6 Then
LrowRD = 6
End If
Sheets("Employees").Range("D" & LrowRD).Value = ActiveSheet.Range("D1").Value
Sheets("Employees").Range("E" & LrowRD).Value = ActiveSheet.Range("E" & TRow).Value
Sheets("Employees").Range("F" & LrowRD).Value = ActiveSheet.Range("W" & TRow).Value - ActiveSheet.Range("W" & TRow).Value - ActiveSheet.Range("W" & TRow).Value
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Add this code to EVERY sheet in your workbook...


Code:
Private Sub Worksheet_Activate()
    curSheet = ActiveSheet.Name
End Sub
Private Sub Worksheet_Deactivate()
    Exit_Code curSheet
End Sub


And now add the below code to any other module in your project...

Code:
Public curSheet
Function Exit_Code(shName)
    MsgBox shName & " says, BYE!"
End Function



This should get you there..or close enough to finish it off.
 
Upvote 0
HI Steve,
Sure, I'll play around with it look like it should work great thanks

Tony
 
Upvote 0
Another option, rather than putting code in each sheet, just put this in the ThisWorkbook module
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Call Employees(Sh)
End Sub
And use this for the sub
Code:
Sub Employees(Ws As Worksheet)
TRow = 64
If Ws.Range("Y" & TRow) = "Acceptable" Then Exit Sub
LrowRD = Sheets("Employees").Cells(Rows.Count, "D").End(xlUp).Row + 1
If LrowRD < 6 Then
LrowRD = 6
End If
Sheets("Employees").Range("D" & LrowRD).Value = Ws.Range("D1").Value
Sheets("Employees").Range("E" & LrowRD).Value = Ws.Range("E" & TRow).Value
Sheets("Employees").Range("F" & LrowRD).Value = Ws.Range("W" & TRow).Value - Ws.Range("W" & TRow).Value - Ws.Range("W" & TRow).Value
End Sub
 
Upvote 0
Another option, rather than putting code in each sheet, just put this in the ThisWorkbook module
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Call Employees(Sh)
End Sub



This is actually the PERFECT solution. I have had issues with it before though when programmatically switching between sheets.
 
Upvote 0
That's just one of the draw-backs of any Auto Event and why I tend to use them sparingly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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