Changing code name of worksheet

Night_Rain

Board Regular
Joined
Jan 1, 2005
Messages
181
Using VBA, how can I change the code name of a worksheet at runtime, as opposed to the display name of the worksheet (Worksheet(x).Name = "blahblahblah")?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi Night_Rain,

The codename of a worksheet cannot be changed programmatically at run time. There is a good reason for this--it could make other code that has already been scanned by the VBA engine for correct object referencing become invalid at run time.

Damon
 
Upvote 0
Using VBA, how can I change the code name of a worksheet at runtime...
Well, let's say you have a workbook with 10 sheets and one of them has the codename Sheet5. This macro will change that sheet's codename to Sheet005:

Sub ChangeCodeName()
ThisWorkbook.VBProject.VBComponents(Sheet5.CodeName).Properties("_CodeName") = "Sheet005"
End Sub


Use this with caution, as subsequent code in your project will fail if it depends on a codename property of Sheet5 for that worksheet object.
 
Upvote 0
programmatically changing worksheet codename

Thanks Tom,

I didn't think that was possible based on what it says in the help files. I learned something.

Damon
 
Upvote 0
Hi Damon - -

Thanks for the note. I just now looked at Help for the CodeName property and I see what you mean. I gave up on Help a few years ago, after coming across too many factual errors. Help is definitely an area where Microsoft can improve its Excel product in my opinion.
 
Upvote 0
Gotta Say, great answers here.
However i wanted to ask sth. For Future People

Why is Tom Urtis using .Properties("_CodeName") when the VbComponents itemList already contains the CodeName with the name .name?
Am i overlooking any edgecase / usecase?

ThisWorkbook.VbProject.VBComponents(worksheet.CodeName).Name = "CodeNameHere"
 
Upvote 0

Forum statistics

Threads
1,221,846
Messages
6,162,378
Members
451,760
Latest member
samue Thon Ajaladin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top