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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
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.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,480
Messages
6,125,048
Members
449,206
Latest member
Healthydogs

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