Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA set sheet name at auto_open routine

Posted on 2013-01-05
3
Medium Priority
?
653 Views
Last Modified: 2013-01-05
Hello Experts.

I have my auto_open routine set - but I want to add additional code to ensure that my sheet names were not changed by an enduser.  If they were changed - then I want to reset them back to their original name on open.  I do not have the workbook protected, just the individual sheets.

I have 5 worksheets where the name should stay the same.  (The code.names are no longer sheet1 sheet 2) The code.names are set as follows:

week1, week2, week3, week4 & week5

In my open routine - I want to rename any worksheet that may have been changed by referencing it by it's codename. Listed below are the code.name with the respective worksheet names.

week1 = "week 1"
week2 = "week 2"
etc. etc.

Is this possible to do this?
Thank you,
Michael
0
Comment
Question by:mike637
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 2000 total points
ID: 38747481
Hi, Michael.

Please see attached. The code is...
Option Explicit

Sub Auto_Open()
Dim xSheet As Worksheet

On Error Resume Next

    For Each xSheet In Worksheets
        Select Case xSheet.CodeName
            Case "week1": xSheet.Name = "week 1"
            Case "week2": xSheet.Name = "week 2"
            Case "week3": xSheet.Name = "week 3"
            Case "week4": xSheet.Name = "week 4"
            Case "week5": xSheet.Name = "week 5"
        End Select
    Next

On Error GoTo 0
    
End Sub

Open in new window

Regards,
Brian.
Five-Weeks.xls
0
 

Author Closing Comment

by:mike637
ID: 38747498
AWESOME!!

Thank you Brian!
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38747499
Thanks, Michael!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question