Home » excel » excel – Returning to starting worksheet

excel – Returning to starting worksheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve recorded a quick macro which moves from my starting worksheet to a different worksheet, copies some cells and then goes back to the original worksheet to paste the contents of the copied cells.
While recording the macro the worksheet had a certain name and I’m trying to understand how to change it so that the macro will return to whatever worksheet I was on when initiating the macro and not returning to a specifically named macro.
This is what the code looks like:

Sheets("vlookup template").Select
Range("A1:K1").Select
Selection.Copy
Sheets("Sheet8").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("vlookup template").Select
Range("B2:K2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet8").Select
Range("B2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("B2:K11")
Range("B2:K11").Select
Selection.Copy

I want to change it so that instead of going to ‘sheet8’ it returns to the original sheet.

How to&Answers:
Dim homeSheet As WorkSheet
Set homeSheet = ActiveSheet

'.... Do stuff

homeSheet.Activate

Please see Avoid Select and Activate for more information.