Google Answers Logo
View Question
 
Q: Excel macro to change worksheet focus ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: Excel macro to change worksheet focus
Category: Computers > Programming
Asked by: screamv-ga
List Price: $2.00
Posted: 22 Aug 2006 19:21 PDT
Expires: 21 Sep 2006 19:21 PDT
Question ID: 758596
I have an Excel macro that creates new worksheets in the workbook.
After the new worksheets are created, the focus is on the last newly
created worksheet, but I would like it to be on the original
worksheet. What line of code can I add to the end of my macro to
accomplish this?
Answer  
Subject: Re: Excel macro to change worksheet focus
Answered By: maniac-ga on 23 Aug 2006 16:26 PDT
Rated:5 out of 5 stars
 
Hello Screamv,

The comment is a little incomplete, so let's expand on it slightly.

Adding a line near the top of the macro like
  oldSheet$ = ActiveSheet.Name
will capture the name of the sheet currently active (so the macro does
not need to assume a specific name).

Adding a line near the end like
  Sheets(oldSheet$).Activate
will activate the original worksheet using the name captured previously.

A complete example is something like the following:
Sub addSheet()
'
'   macro to add a sheet & return to the original one
'
    oldSheet$ = ActiveSheet.Name
    Worksheets.Add
    Worksheets(oldSheet$).Activate

End Sub

This adds a sheet prior to the currently selected sheet & then
activates that original worksheet.

Please make a clarification request if any part of the answer is
unclear or you need additional information to utilize this answer in
your worksheets. I would be glad to help.

Good luck with your work.

  --Maniac
screamv-ga rated this answer:5 out of 5 stars
Thanks, nice enhancement to the comment already posted.

Comments  
Subject: Re: Excel macro to change worksheet focus
From: agent_84-ga on 23 Aug 2006 01:03 PDT
 
Hi, noproblem ...

Sheets("Sheet1").Select     // "Sheet1" name of sheet that will be active
Subject: Re: Excel macro to change worksheet focus
From: screamv-ga on 23 Aug 2006 20:25 PDT
 
Thanks! That did the trick.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy