After worksheet codename change the object does not exist

hombibi

New Member
Joined
Aug 2, 2017
Messages
12
Following the approach in Change Worksheet Codename

I found that the object can not be called with its new name in the same execution.

VBA Code:
Option Explicit

Private Sub test_codename()

Dim ws As Worksheet

Set ws = Sheets(1)
ws.[_CodeName] = "Sheet1"
Set ws = Sheets("Sheet1")
Debug.Print ws.CodeName
Debug.Print Sheet1.CodeName
Sheet1.Activate

Set ws = Sheets(1)
ws.[_CodeName] = "CodeNameTestTwo"
Set ws = Sheets(1)
Debug.Print ws.CodeName
Debug.Print CodeNameTestTwo.CodeName
CodeNameTestTwo.Activate

End Sub

Consider the code below for a standard worksheet, the first Codename change does nothing, but confirmes the code,
The second iteration fails with variable not set.
It looks as if they compiler trips as the new object is not available yet at compile time.

Anyone know a way to solve this?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

hombibi

New Member
Joined
Aug 2, 2017
Messages
12
I figured it out: both objects need to be declared and the assigned,

Dim ws As Worksheet
Set ws = Sheets(1)
Dim CodeNameTestTwo As Worksheet
Set CodeNameTestTwo = Sheets(1)

Solved.
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,167
Messages
5,640,534
Members
417,151
Latest member
ChickenTenderer

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
Top