Hi everyone,
I use the following code (found there) to switch back and forth between the last worksheet showed and the current one (so like alt-tab but between the sheets as opposed to between workbooks). I was wondering how the code could be tweaked so that it switches between sheets within the same workbook when you have multiple workbooks open. Eg you have 2 workbooks: workbook 1 with sheets 1, 2; workbook 2 with sheets 1, 2. When going from workbook 1 sheet 1, then sheet 2 to workbook 2 sheet 1 then back to workbook 1 sheet 1 pressing the macro would take me to workbook 1 sheet 2 (and not workbook 2 sheet 2). Any idea how to do the tweak please? Many thanks!
I use the following code (found there) to switch back and forth between the last worksheet showed and the current one (so like alt-tab but between the sheets as opposed to between workbooks). I was wondering how the code could be tweaked so that it switches between sheets within the same workbook when you have multiple workbooks open. Eg you have 2 workbooks: workbook 1 with sheets 1, 2; workbook 2 with sheets 1, 2. When going from workbook 1 sheet 1, then sheet 2 to workbook 2 sheet 1 then back to workbook 1 sheet 1 pressing the macro would take me to workbook 1 sheet 2 (and not workbook 2 sheet 2). Any idea how to do the tweak please? Many thanks!
VBA Code:
Dim TabTracker As New TabBack_Class
Sub TabBack_Run()
'PURPOSE: Initiate Tab tracking and shortcut key trigger
'SOURCE: www.TheSpreadsheetGuru.com
'Enable TabTracker class
Set TabTracker.AppEvent = Application
'Call ToggleBack macro when user keys alt + `
Application.OnKey "%`", "ToggleBack"
End Sub
Sub ToggleBack()
'PURPOSE: Go Back to Previous Worksheet
'SOURCE: www.TheSpreadsheetGuru.com
With TabTracker
On Error Resume Next
Workbooks(.WorkbookReference).Worksheets(.SheetReference).Activate
On Error GoTo 0
End With
End Sub
VBA Code:
Public WithEvents AppEvent As Application
Public SheetReference As String
Public WorkbookReference As String
Private Sub AppEvent_SheetDeactivate(ByVal Sh As Object)
'PURPOSE: Store active worksheet information before leaving it
'SOURCE: www.TheSpreadsheetGuru.com
WorkbookReference = Sh.Parent.Name
SheetReference = Sh.Name
End Sub
Private Sub AppEvent_WorkbookDeactivate(ByVal Wb As Workbook)
'PURPOSE: Store active worksheet information before closing workbook
'SOURCE: www.TheSpreadsheetGuru.com
WorkbookReference = Wb.Name
SheetReference = Wb.ActiveSheet.Name