Hi all. I need some help. I have the following code below that allows me to switch back and forth between the active worksheet and the previous worksheet. However, to do this, the macros are stored in the ThisWorkbook module and a standard module of a specific file. What I want to do is store the macros in the personal.xls file so I it will work with any workbook I open.
Stored in ThisWorkbook:
Private Sub Workbook_Open()
mySheetName = Workbook.ActiveSheet.Name
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
OldSheetName = mySheetName
mySheetName = ActiveWorkbook.ActiveSheet.Name
End Sub
Stored in Module 1:
Public OldSheetName As String
Public mySheetName As String
Sub ReturnToLastSheet()
Worksheets(OldSheetName).Activate
End Sub
I have copied this to the personal.xls workbook and messed around with it, but I think the problem is when I select a sheet on the active workbook (not personal.xls) it doesn't run the SheetActivate procedure which is located in ThisWorkbook of personal.xls as opposed to the ThisWorkbook in the active workbook.
When I run the ReturnToLastSheet macro, it allways results in an error 9 because OldSheetName= "" as opposed to an actual sheet name, I beleive, due to the reference of OldSheetName = mySheetName being in ThisWorkbook of personal.xls i.e. the variable (sheet name) is never stored.
The goal here is to not have the macros in personal.xls as to not copy the macros in to each and every workbook I work with. How can the macros be altered in personal.xls so that it works for any workbook, i.e. I can swith back and forth bewteen the current and previous sheet in the active workbook?? Please help.
Stored in ThisWorkbook:
Private Sub Workbook_Open()
mySheetName = Workbook.ActiveSheet.Name
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
OldSheetName = mySheetName
mySheetName = ActiveWorkbook.ActiveSheet.Name
End Sub
Stored in Module 1:
Public OldSheetName As String
Public mySheetName As String
Sub ReturnToLastSheet()
Worksheets(OldSheetName).Activate
End Sub
I have copied this to the personal.xls workbook and messed around with it, but I think the problem is when I select a sheet on the active workbook (not personal.xls) it doesn't run the SheetActivate procedure which is located in ThisWorkbook of personal.xls as opposed to the ThisWorkbook in the active workbook.
When I run the ReturnToLastSheet macro, it allways results in an error 9 because OldSheetName= "" as opposed to an actual sheet name, I beleive, due to the reference of OldSheetName = mySheetName being in ThisWorkbook of personal.xls i.e. the variable (sheet name) is never stored.
The goal here is to not have the macros in personal.xls as to not copy the macros in to each and every workbook I work with. How can the macros be altered in personal.xls so that it works for any workbook, i.e. I can swith back and forth bewteen the current and previous sheet in the active workbook?? Please help.