Reference Worksheet Codename in a loop

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I found this reference a believe it's what I need, but not sure how to pass the codename to the function to get the object.


I've created three sheets with codename like, ws1, ws2, ws3. I would like to run a loop to those sheets using the first two letters (ws) combined with the number.

I've hit a wall with this

VBA Code:
Sub addvalue()
Dim mySheet As String
Dim i As Long
For i = 1 To 3
mySheet = "ws" & i
SheetFromCodeName (mySheet)
mySheet.Range("A1").Value = "Yes"
Next i
End Sub

VBA Code:
Function SheetFromCodeName(aName As String, Optional wb As Workbook) As Worksheet
'https://www.ozgrid.com/forum/index.php?thread/132768-reference-worksheet-codename-using-a-string-variable/
If wb Is Nothing Then Set wb = ThisWorkbook
With wb
Set SheetFromCodeName = .Sheets(.VBProject.VBComponents(aName).Properties("Index"))
End With
End Function
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Why not just use
VBA Code:
Sheets(mySheet).Range("A1").Value
?


I found this reference a believe it's what I need, but not sure how to pass the codename to the function to get the object.


I've created three sheets with codename like, ws1, ws2, ws3. I would like to run a loop to those sheets using the first two letters (ws) combined with the number.

I've hit a wall with this

VBA Code:
Sub addvalue()
Dim mySheet As String
Dim i As Long
For i = 1 To 3
mySheet = "ws" & i
SheetFromCodeName (mySheet)
mySheet.Range("A1").Value = "Yes"
Next i
End Sub

VBA Code:
Function SheetFromCodeName(aName As String, Optional wb As Workbook) As Worksheet
'https://www.ozgrid.com/forum/index.php?thread/132768-reference-worksheet-codename-using-a-string-variable/
If wb Is Nothing Then Set wb = ThisWorkbook
With wb
Set SheetFromCodeName = .Sheets(.VBProject.VBComponents(aName).Properties("Index"))
End With
End Function
 
Upvote 0
Wouldn't that get me the Sheet name when I'm looking for the codename. I tried that and I get "subscript out of range"

VBA Code:
Sub addvalue()
    Dim mySheet As String
    Dim i As Long
    For i = 1 To 3
        mySheet = "ws" & i
        Sheets(mySheet).Range("A1").Value = "Yes"
    Next i
End Sub
 
Upvote 0
Do you need to use their code name? If so, then try with your earlier function.
VBA Code:
Sub addvalue()
  Dim wSheet As String, mySheet As Variant
  Dim i As Long
  For i = 1 To 3
    wSheet = "ws" & i
    Set mySheet = SheetFromCodeName(wSheet)
    Sheets(mySheet).Range("A1").Value = "Yes"
  Next i
  Set mySheet = Nothing
End Sub

Also you will need to make sure the Macro Settings // Developer Macro Settings // Trust access to the VBA project object model CHECKBOX has been checked to use your code above...
 
Last edited:
Upvote 0
Thank you CSmith. One change and it worked great.

Instead of
VBA Code:
Sheets(mySheet).Range("A1").Value = "Yes"

Change to
VBA Code:
mySheet.Range("A1").Value = "Yes"
 
Upvote 0
Opps ya that was copy paste error on my part... I used Debug.Print line... Glad to help :) Thanks for the update! My test code ;)
VBA Code:
Sub addvalue()
  Dim wSheet As String, mySheet As Variant
  Dim i As Long
  For i = 1 To 3
    wSheet = "Sheet" & i
    Set mySheet = SheetFromCodeName(wSheet)
    Debug.Print mySheet.Name
  Next i
  Set mySheet = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,899
Messages
6,122,155
Members
449,068
Latest member
shiz11713

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