How to return to the worksheet a macro was called from?

Tom picture Tom · Feb 2, 2017 · Viewed 15.8k times · Source

I have a macro that is duplicated on buttons on 3 worksheets which moves between worksheets and at the moment once the macro command is completed I have it set to return to the 3rd sheet every time. Is there a command I can put in the code that would return the view to the sheet that the macro was activated on in the first place rather than a set sheet.

Answer

Mafii picture Mafii · Feb 2, 2017

Save the active sheet in a variable, then, after your macro finished, select the sheet within the variable again:

' at the start of your macro:
Dim sourceSheet as Worksheet
set sourceSheet = ActiveSheet


' at the end of your macro:
Call sourceSheet.Activate