Help referring to a sheet when I don't know the name of it

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
308
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
In my workbook, the names of some of the worksheets sometimes change. However, the sheet "numbers" never change.

I need to assign the name of a particular sheet to a variable, so I can refer to the name later on in code. I figured out how to do it, using this:

Code:
Dim NameOfSheet As String

NameOfSheet = Sheet28.Name

However, my understanding is I should always put ThisWorkbook in front of any sheet reference, in case another workbook is open at the time the code is running, because I don't want the variable to be assigned the name of Sheet28 on the other open sheet (assuming it has one).

The way I would have thought to do it is like this:

Code:
NameOfSheet = ThisWorkbook.Sheet28.Name

Unfortunately, this causes an error. Is there a way to refer to the actual sheet "number" (like Sheet28 above) and ALSO tell it that I am referring to ThisWorkbook?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe this helps a little

Code:
    Application.ScreenUpdating = False
    wb = ActiveWorkbook.Name
    ThisWorkbook.Activate
    NameOfSheet = Hoja28.Name
    Workbooks(wb).Activate
    Application.ScreenUpdating = True
 
Upvote 0
Another option


Code:
    Dim sh As Worksheet
    For Each sh In ThisWorkbook.Sheets
        If sh.CodeName = "Hoja28" Then
            NameOfSheet = sh.Name
            Exit For
        End If
    Next
 
Upvote 0
However, my understanding is I should always put ThisWorkbook in front of any sheet reference

@mcomp72, sorry for asking this but who has told you this? the sheets codename only applies to ThisWorkbook (it is its one drawback), unless you use some convoluted code (see below for how to refer to a different workbook using its codename and you will see what I mean) and so it really isn't necessary to refer to ThisWorkbook when using it's codename.

some convoluted code
Code:
Workbooks("OtherWorkbook.xlsm").VBProject.vbcomponents("Sheet1").Properties("name").Value
 
Last edited:
Upvote 0
Too late to edit the previous post but to refer to a cell using it you would need....
Code:
Workbooks("OtherWorkbook.xlsm").Sheets(Workbooks("OtherWorkbook.xlsm").VBProject.vbcomponents("Sheet1").Properties("name").Value).Range("A1")
 
Upvote 0
... Is there a way to refer to the actual sheet "number" (like Sheet28 above) and ALSO tell it that I am referring to ThisWorkbook?
The codename Sheet28 refers to the sheet in ThisWorkbook even if it is not the active workbook.
 
Upvote 0
Thanks to all of you for your replies.

I did not know that if I used the codename Sheet28 it would automatically refer to ThisWorkbook. That is great to know.

In my coding, I generally refer to sheet names by their actual name, such as:

Code:
With ThisWorkbook.Sheets("Sheet1")

   ...some code...

End With

In this instance, is it necessary for me to put ThisWorkbook? Or is "Sheet1" automatically going to refer to ThisWorkbook?
 
Upvote 0
From the Excel help: "Sheets Object: A collection of all the sheets in the specified or active workbook." So, if the parent is not explicitly specified, the Sheets("Sheet1") refers to the sheet in ActiveWorkbook.
 
Upvote 0
Just to be clear the sheets codename is unique in that it refers to the sheet the code resides in i.e. ThisWorkook not the ActiveWorkbook unlike the other ways of referencing the sheet i.e Index or Name.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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