Assigning Worksheet Variable Using Sheet CodeName

masouder

Board Regular
Joined
Jul 5, 2013
Messages
111
Office Version
  1. 2013
Platform
  1. Windows
I am trying to set a worksheet variable using the worksheet CodeName property which is stored in a cell in the workbook. If I wanted to do this using the worksheet Name variable that is easy as evidenced by the code below, where the sheet Name is stored in cell A1.

VBA Code:
Sub Test()
    Dim ws As Worksheet
    
    Dim sSheet As String
    
    sSheet = [A1]
    
    MsgBox Worksheets(sSheet).Name
    
End Sub

If, however, I store the sheet CodeName in cell A1, then no matter what I try I cannot use it to assign a worksheet variable.

Any ideas are appreciated.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this

VBA Code:
Sub Test()

Dim sheetname As String
sheetname = Worksheets("Sheet1").Range("A1").Value
   
MsgBox sheetname
    
End Sub
 
Upvote 0
@masouder,

Change your code:

VBA Code:
    Dim sSheet As String

to:

VBA Code:
    Dim sSheet As Long

Then set A1 to the index number of sheet that you desire. ;)
 
Upvote 0
The most common way of getting the sheet if you only have the codename as a string seems to be to use a loop.
VBA Code:
Sub getCodeName()

    Dim ws As Worksheet
    Dim sSheet As String
    
    sSheet = [A1]

    For Each ws In ActiveWorkbook.Worksheets
        If ws.CodeName = Range("A1").Value Then
            sSheet = ws.Name
            Exit For
        End If
    Next ws
    
    MsgBox "Sheet Name: " & Worksheets(sSheet).Name & " / CodeName: " & Worksheets(sSheet).CodeName

End Sub
 
Upvote 0
Thank you all for the replies. Unfortunately, the first suggestion (earthworm) does not work because it requires the sheet Name property, not the CodeName property. Similarly, the second suggestion (johhnyL) also won't work because it relies on the Index property.

Alex, I suspected that I would have to use a looping routine based on other posts, but was hoping that there was another way. Unless anyone else has a suggestion on how to set the worksheet variable using on the CodeName property, I guess I'll have to use the loop.
 
Upvote 0
If you have trusted access to the vbproject, you can use:

Code:
Set ws = ThisWorkbook.Sheets(ThisWorkbook.VBProject.VBComponents(sSheet).Properties("Name").Value)
 
Upvote 0
Solution
If you have trusted access to the vbproject, you can use:

Code:
Set ws = ThisWorkbook.Sheets(ThisWorkbook.VBProject.VBComponents(sSheet).Properties("Name").Value)

I think we should add that turning on "trusted access to the vbproject" is not a recommended setting from a security perspective and you can't count on your end user having it turned on.
 
Upvote 0
The problem I am encountering is that it doesn't seem to be possible to use a variable/Cell refernce as the codename, without using the previously mentioned security route.

VBA Code:
msgbox TheCodeNameOfSheet.name

That gives the sheet name, but I can't find a way to use a variable for the codename. :(
 
Upvote 0
Should we also add that enabling macros is not recommended from a security perspective? :)
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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