ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Return to current worksheet (https://www.excelbanter.com/excel-programming/357379-return-current-worksheet.html)

Patrick Simonds

Return to current worksheet
 
I have a macro which when it runs cycle through all the worksheets in my
workbook. When it finishes it leaves me on the last worksheet of the
workbook. I would like to be returned to the worksheet that was active when
the macro was started.



Tom Ogilvy

Return to current worksheet
 
Sub GoBackHomeDemo()
Dim sh as Worksheet, sh1 as Worksheet
set sh1 = Activesheet
for each sh in Worksheets
sh.Activate
Next
sh1.Activate
End Sub


--
Regards,
Tom Ogilvy


"Patrick Simonds" wrote:

I have a macro which when it runs cycle through all the worksheets in my
workbook. When it finishes it leaves me on the last worksheet of the
workbook. I would like to be returned to the worksheet that was active when
the macro was started.




[email protected]

Return to current worksheet
 
Define a variable as a string to capture your macro workbook name

Dim MainWB as string

MainWB=ActiveWorkbook.Name 'place this somewhere near the beginning
of your macro

......after sub runs, near end of macro

Windows(MainWB).Activate

....for more specifics, you could also add the following
Sheets("insertSheetNameHere").Select
Range("A1").Select

---Joran6



All times are GMT +1. The time now is 03:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com