Using Codename In A Loop - How To Refer?

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hey,

So I've started to pick up on using codenames when writing scripts, majority of them I can pull them off or learn the workaround but in my example.. say I have 10 sheets, all codenames are sheet1, sheet2, sheet3 etc.. why does this code not work when referring? Why does it not pick up as Sheet2.Delete?

If anyone can give me guidance on codenames e.g. is it the preferred way, is it the most efficient, what are the cons to using vs sheet name and index other than it can pinpoint a sheet more accurately where sheet names and indexs can change constantly.

**Just to note, I do not want to refer by their index OR tab name.

Code:
Sub test()

Application.ScreenUpdating = False

For I = 2 To 10
  Sheet & I.Delete
Next

Application.ScreenUpdating = True

End Sub
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You can't do that simply because you cannot create variable names (which is essentially what a code name is) by concatenating two strings together. You could use an array of the actual code names and loop through that or, if you have trusted access to the VBProject, you can loop through the VBComponents using the code names as strings.
 
Upvote 0
You can't do that simply because you cannot create variable names (which is essentially what a code name is) by concatenating two strings together. You could use an array of the actual code names and loop through that or, if you have trusted access to the VBProject, you can loop through the VBComponents using the code names as strings.

Thanks, so something like
Code:
For Each sh in Sheets(Array(Sheet1.Name, Sheet2.Name, Sheet3.Name, Sheet4.Name))
'do something here
Next Sh

Which leads me to another challenge, if that full array doesn't exist e.g. Sheet2 is there but Sheet1 and Sheet3 isn't then you get a 424 Object Required error.. what's the work around on that?
 
Last edited:
Upvote 0
Maybe:
Rich (BB code):
Dim shtNames As Variant: shtNames = Array("Sheet1", "Sheet2", "Sheet3")

For Each ws In ThisWorkbook.Worksheets
    If UBound(Filter(shtNames, ws.CodeName)) >= 1 Then
        Debug.Print ws.CodeName
    End If
Next ws
 
Upvote 0
If the sheet might not be there, you probably shouldn't be using code names in all honesty.
 
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