vb script to go back to last sheet viewed

steve2115

Board Regular
Joined
Mar 17, 2014
Messages
82
I am looking for a script that will take users back to the last sheet viewed/edited after a "delete macro runs" (not the last sheet in the workbook - previous sheet viewed/edited)

Below is script that I have that deletes the records on the "Returns" sheet

Sub Macro2()
Sheets("Returns").Select
ActiveWindow.SmallScroll Down:=-6
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
End Sub

My next would be to "jump" back to the last sheet that was viewed/edited. There are mutiple sheets in the workbook so I can not use the sheet names as I would not know exactly what sheet users last viewed.

Any help would be greatly appreciated.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try storing the active sheet at the beginning of the macro and then restore it at the end.

Beginning of the code:
Code:
    Dim wsLast As Worksheet
 

    wsLast = ActiveSheet

End of the code:
Code:
    wsLast.Activate
 
Upvote 0
I'm getting a Compile Error: Invalid outside procedure
very new to this so mayeb I did not add the code correctly. Here is how it currently looks.

Dim wsLast As Worksheet

wsLast = ActiveSheet
Sub Macro2()
Sheets("Returns").Select
ActiveWindow.SmallScroll Down:=-6
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
wsLast.Activate
End Sub


 
Upvote 0
It has to be placed within the sub, try this:

Code:
Sub Macro2()
 

    Dim wsLast As Worksheet

    wsLast = ActiveSheet
    

    Sheets("Returns").Select
    ActiveWindow.SmallScroll Down:=-6
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    

    wsLast.Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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