Worksheet CodeName as type Worksheet

RobOrBob

New Member
Joined
Aug 4, 2023
Messages
41
Office Version
  1. 365
Platform
  1. Windows
I have a .xlsm that has data feeds pushed into a specific workbook/sheet and uses Worksheet_Change to perform various actions as the data changes.

So my VBA has to be very specific about the workbook/sheet references as the active worksheet may be another in the workbook or even a different workbook entirely.

The following is a test piece that demonstrates the problem (it clears the texts of some dummy form buttons).

Rather than protecting the worksheet, I assumed that by setting the '(Name)' property (aka CodeName) it would protect the code from changes to the display worksheet name 'Name' (which the user can change from the tab):
Screenshot 2024-04-07 223244.png


In VBA Project>Modules>Module1 I have these declarations:
VBA Code:
Option Explicit
    Public wb As Workbook
    Public ws As Worksheet

In VBA Project>Microsoft Excel Objects>ThisWorkbook I have this code:
VBA Code:
Private Sub Workbook_Open()
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Interface")
    ws.Buttons.Text = ""
End Sub

On opening the workbook I get a 'Run-time error '9' Subscript out of range' on the Set ws line; if I change it to:
VBA Code:
Set ws = wb.Worksheets("Sheet1")
.... then when I open the workbook again, all is well.

Any ideas?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The error message is because there is no worksheet with a name (not codename) "Interface"

You just need to say:

VBA Code:
Set ws = Interface
 
Upvote 0
You've actually got the sheet code name & sheet tab name back to front. In your example, "Interface" is the code name of the sheet and "Sheet1" is the tab name. Using your code, try:

VBA Code:
Private Sub Workbook_Open()
    Dim ws as Worksheet
    Set ws = Interface
    ws.Buttons.Text = ""
End Sub
 
Upvote 0
The error message is because there is no worksheet with a name (not codename) "Interface"

You just need to say:

VBA Code:
Set ws = Interface
Thanks for the reply.

That works but does that not default to Interface in the current active workbook (which I'm trying to avoid)?
 
Upvote 0
You've actually got the sheet code name & sheet tab name back to front. In your example, "Interface" is the code name of the sheet and "Sheet1" is the tab name. Using your code, try:

VBA Code:
Private Sub Workbook_Open()
    Dim ws as Worksheet
    Set ws = Interface
    ws.Buttons.Text = ""
End Sub
Thank you for your response.

I think thats what I originally said, I should have given the actual texts to clarify the context.

But as per my reply to Stephen, I don't see how
VBA Code:
Set ws = Interface
doesn't reference the current active workbook as its not qualified.

I'm not sure I even understand how Interface seems to be automatically defined either.
 
Upvote 0
That works but does that not default to Interface in the current active workbook (which I'm trying to avoid)?
An unqualified reference to a sheet by its name will refer to the ActiveWorkbook

But a sheet's codename will refer to the workbook with the code in it, ThisWorkbook.

Give it a try!
 
Upvote 0
Solution
An unqualified reference to a sheet by its name will refer to the ActiveWorkbook

But a sheet's codename will refer to the workbook with the code in it, ThisWorkbook.
Tested and it does exactly that! Who would have thunk it!

Many thanks to you and Kevin.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,659
Members
449,114
Latest member
aides

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