VBA To Return To Last Sheet

JADownie

Active Member
Joined
Dec 11, 2007
Messages
395
I have a workbook with 8 sheets. On each department sheet, I have hyperlinks which direct the user to the raw data from which the report was created.

I wanted to have a button on my raw data sheet which would be a "Go Back" button and take the user back to last sheet they were on (when they clicked the hyperlink).

I have the code below that does this to some extent, but I cannot get it to work for my situation here. This code I believe references the last worksheet clicked on, and ignores the hyperlink all together.

Is it possible to tweak my code below to take into consideration hyperlinks?



I have the below macro which is executed by pressing a form button:

Global WhtSheet As String, LastSheet As String
Sub GoBack()
Sheets(LastSheet).Select
End Sub

Additionally, I have the code below on each sheet in my workbook.

Private Sub Worksheet_Activate()
LastSheet = WhtSheet
WhtSheet = ActiveSheet.Name
End Sub


Thanks for any assistance or feedback with this request.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Maybe like this in a regular module:

Code:
Global LastSheet As String

Sub GoBack()
   Sheets(LastSheet).Activate
End Sub

Then put this in the ThisWorkbook module:
Code:
Sub Workbook_SheetDeactivate(ByVal sh As Object)
    LastSheet = sh.Name
End Sub

This is all you need for the GoBack macro to work, no need to put macros in every sheet module.
 
Upvote 0
I am getting a runtime error here:



Sub GoBack()
Sheets(LastSheet).Activate
End Sub


But I am not sure why....
 
Upvote 0
Maybe like this in a regular module:

Code:
Global LastSheet As String

Sub GoBack()
   Sheets(LastSheet).Activate
End Sub

Then put this in the ThisWorkbook module:
Code:
Sub Workbook_SheetDeactivate(ByVal sh As Object)
    LastSheet = sh.Name
End Sub

This is all you need for the GoBack macro to work, no need to put macros in every sheet module.

This worked great for me, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,944
Members
449,198
Latest member
MhammadishaqKhan

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