View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
sebastienm sebastienm is offline
external usenet poster
 
Posts: 694
Default Return to previous worksheet

Hi,
You can use a small class to track the sheet Deactivate event of the
Application object. In the Personal book:
- Add a class module and call it ClsSheetTracker. Paste the following code
ion this class module
' ------------------------------------------------------
Option Explicit

Private WithEvents App As Application
Private mLastSheet As Object

'------------------------------------
Public Property Get LastSheet() As Object
Dim s As String
If mLastSheet Is Nothing Then
Set LastSheet = Nothing
Else
On Error Resume Next 'capture automation error if book has been
closed
s = mLastSheet.Name
If Err < 0 Then
Set mLastSheet = Nothing
Set LastSheet = Nothing
Else
Set LastSheet = mLastSheet
End If
End If
End Property

Public Sub GotoLastSheet()
If Not LastSheet Is Nothing Then
On Error Resume Next 'if hidden book or other error
LastSheet.Parent.Activate
LastSheet.Activate
End If
End Sub
'------------------------------------

Private Sub App_SheetDeactivate(ByVal Sh As Object)
Set mLastSheet = Sh
End Sub

Private Sub Class_Initialize()
Set App = Application
End Sub

Private Sub Class_Terminate()
Set App = Nothing
End Sub
'-----------------------------------------------------

As you can see in the above code, when a sheet is Deactivated the mLastSheet
variable is set, keeping track of that deactivated sheet.
Also there is 2 public methods, one that returns the Last Sheet, the other
one is a sub (GotoLastSheet) that activates the last sheet.

- Now in a regular module, create a public variable of the above class and
add a Sub that calls the GotoLastSheet of the above class.

'----------------------------------------------------
Option Explicit
Public Tracker As ClsSheetTracker

Sub GotoLast()
Tracker.GotoLastSheet
End Sub
'----------------------------------------------------

--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"Mats Samson" wrote:

Hi,
I have two workbooks (WTNSystem and WTNDatabase) open, with several sheets
in each workbook.
Id like to create a Return-buttons in the sheets that takes me back to the
previous sheet, even if it was in the other workbook. The originating sheet
can be different each time too.
I know that Public variables arent particularly reliable but maybe there is
a solution?! Furthermore, Id like to keep this code in the hidden
Personal.xls, so the code can be used by any sheet calling it.
Thanks
Mats