Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA set sheet name at auto_open routine

Posted on 2013-01-05
3
Medium Priority
?
644 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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 …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

715 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