I similarly am having trouble like
@SaraWitch to get the examples from the previous page to work when trying to use VBA to move to the last active sheet
I've followed the instructions and have added the following to my "ThisWorkbook" sheet inside the VBA editor:
VBA Code:
Public lastsheet As String
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
' MsgBox Prompt:="You just left sheet:" & Sh.Name
lastsheet = Sh.Name
End Sub
and have created a new module with the following text inside of it that is meant to make the workbook return to the previous sheet:
VBA Code:
Sub ReturnPrevious(Control As IRibbonControl)
'On Error GoTo ErrHndl
Sheets(lastsheet).Activate
'ErrHndl:
' Exit Sub
End Sub
You'll see that I'm using this macro for a custom ribbon button and also I've commented out some lines to try and make debugging easier.
I've tested both sections using the debugger and both appear to be valid and work by themselves however, when the ReturnPrevious macro runs the "lastsheet" variable is suddenly set to blank and the macro errors out as it can't find the sheet.
Any advice on why the "lastsheet" variable is getting reset? Or perhaps I'm misunderstanding where things are going wrong?