Sheet9.Activate Activates Sheet8 - Why?

ransomedbyfire

Board Regular
Joined
Mar 9, 2011
Messages
121
When I run the following code, for some reason, Excel activates Sheet9 when I tell it to activate Sheet8. I have looked at the sheet properties, and everything seems fine - just not in execution. How can I fix this without having to use a workaround?

Here is a simplified snippet of my code:
Code:
Dim ws as Worksheet
For Each ws in Worksheets

if ws.codename<>"Sheet8" Then
ws.Activate
cname=ws.codename
End If

Next ws

When I run this code, the sheet that is labeled "Sheet8" in the Project window slips through the IF statement uncaught and cname is set to equal "Sheet9".

I have used this code successfully with sheets 1-7; so, it should work here too. But it doesn't.

Can someone please help me with this?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
try
Rich (BB code):
if ws.codename = "Sheet8" Then
 
Upvote 0
I'm just guessing as to the purpose of this code, but if you want a worksheet activated, given its code name you could use.

Code:
Dim sheetsCodeName As String
sheetsCodeName = "Sheet2"

With ThisWorkbook
    .Sheets(.VBProject.VBComponents(sheetsCodeName).Properties("Index")).Activate
End With
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

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