Solved

Excel VBA set sheet name at auto_open routine

Posted on 2013-01-05
3
634 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

628 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