Array of Worksheet CodeNames

VenturSum

Board Regular
Joined
May 23, 2010
Messages
137
All,

I've created an Array of worksheet 'Names', to loop through them.

Instead I'd like to use the 'CodeName' so if the user changes the
name on the tab, the code does not break..

Any suggestions on how to do this?

Code:
Dim vWSArray as Variant, i as long
Dim ws as Worksheet
     
    '- Create the Array --------------------
     vWSArray = Array("01", "02", "03")

     '-- Loop Through Array -----
    For i = LBound(vWSArray) to UBound(vWSArray)
        Set ws = ThisWorkbook.Sheets(vWSArray(i))
        
        With ws
            .
            .
        End With

    Next i

The code: "ThisWorkbook.Sheets()" uses the index number or the name (on the tab)
How can I use the CodeName here? or will it also use the code name?

Thank you,

John,
In Annapolis.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
John

Why not create an array of the sheets?
Code:
vwsArray = Array(Sheet1, Sheet2, Sheet3)

For Each ws In vws
    MsgBox ws.Name
Next ws
 
Upvote 0
Try
Code:
With ThisWorkbook
    Set ws = .Sheets(.VBProject.VBComponents(vWSCodename(i)).Properties("Index").Value)
End With
 
Upvote 0
All,

I've found the best method is to use the .Name attribute.
Thus if a name is changed, On the Tab.... The code continues to work.

See above for balance of code

Code:
    '- Create the Array --------------------
      vWSArray = Array(ws01.Name, ws02.Name, ws03.Name)

Thank you and enjoy,

John Orlando
In Annapolis
 
Upvote 0
It depends on what you are doing with this, but rather than an array of worksheets, creating a Worksheets Object might be usefuel.

Code:
Dim myWorksheets As Sheets
Dim vWSNamesArray As Variant

vWSNamesArray = Array(Sheet1.Name, Sheet2.Name, Sheet3.Name)

Set myWorksheets = Sheets(vWSNamesArray)
MsgBox myWorksheets.Count
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,326
Members
448,564
Latest member
ED38

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