Worksheet Deactivate (process before closing)

cedricthecat

Active Member
Joined
May 17, 2007
Messages
460
Hi folks

Is there a way to use Worksheet_Deactivate to navigate to a particular location in a Worksheet before moving to another Worksheet

After looking at this thread I have realised that a Workbook I have been using with sheet protection allows users to look at "locked" sheets by clicking and holding on the name tab (they can't access the sheets to edit them, but they can view contents - nothing contentious, but I rather they couldn't do even this)

As a work-around I thought I could move to the top of the Worksheet on deactivate (only headers there) and then move back to the current date on activate. The latter works, but the former won't as the action is not being executed on the sheet I'm going to rather than the one I am coming from

I hope this makes sense!

Any help would be appreciated!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi Dave. this seems to work

Code:
Private Sub Worksheet_Deactivate()
Dim ws As Worksheet
Set ws = ActiveSheet
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Goto Me.Range("A1"), True
ws.Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 
Upvote 0
Maybe something like this?

Code:
Private Sub Worksheet_Deactivate()
Ans = MsgBox("Do you wish to run code on the just deactivated sheet, before exiting?", vbQuestion + vbYesNoCancel)
If Ans = vbNo Or Ans = vbCancel Then
' Do nothing allowing user to jump to new sheet
Else
Me.Activate
'enter code here to perform task on the deactivated sheet like (example)...
ActiveSheet.Range("A1").Select


End If
End Sub
 
Upvote 0
Hi Peter

Thanks for this!

Sadly it doesn't like:-

Application.Goto Me.Range("A1"), True

giving me

run-time error '1004'

Method 'Goto' of object '_Application' failed

The sheet is protected but I added an unprotect to the start, so not sure what is going on??

:confused:
 
Upvote 0
Try

Code:
Private Sub Worksheet_Deactivate()
Dim ws As Worksheet
Set ws = ActiveSheet
Application.ScreenUpdating = False
Application.EnableEvents = False
Me.Unprotect
Application.Goto Me.Range("A1"), True
Me.Protect
ws.Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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