Mismatch sheet index

ashley12

New Member
Joined
Dec 6, 2020
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I was trying to refer to my worksheets by their indexes instead of names. However, the numbers I saw on the hierarchy were different from the actual worksheets I got from the codes. What did I get wrong?

For example, attached picture is the list of my sheets. Here is my test code:

VBA Code:
Sub Test()
    Dim ws As Worksheet
      Set ws = ThisWorkbook.Sheets(4)
      MsgBox ws.Name
End Sub

The result is "Database" instead of "Data validation". It happens with all sheet excepts sheet 1.

Thanks in advance.
 

Attachments

  • Screenshot 2021-03-31 173721.png
    Screenshot 2021-03-31 173721.png
    8.6 KB · Views: 7

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
The index is the order they appear in the worksheet, which is not what you are after.
If your code is in the same workbook as the sheets you can use the codename eg
Set ws = Sheet4
but it won't let you create a variable which might be what you are after (does not allow "Sheet" & 4).

What were hoping to achieve by using the index number ?
If its just to make it independent of the name that the user can change, then use the codename.
eg Debug.Print Sheet4.Name
will return Data validation
Ideally you would then use more meaningful code names.

There is more here on not being able to use a variable for a codename (Mr Excel Forum), and if you do need that, on looping through the worksheets to find the sheet with that codename using the syntax - If ws.CodeName = "Sheet" & x Then ...
 
Upvote 0
The sheet index and the sheet's codename are two different things:

Sheets(4) is the 4th sheet in the order shown, which you can also refer to as Sheets("Database") (the sheet name) or Sheet12 (the code name)

Sheets(7) is the 7th sheet in the order shown, which you can also refer to as Sheets("Data validation") (the sheet name) or Sheet4 (the code name).

If you wish, you can use the VB editor to change the codename to something more meaningful than Sheet1, Sheet2 etc.
 
Upvote 0
You did nothing wrong. The index is counting sheet from left to right. Nothing to do with the number shown
 
Upvote 0
Thanks everyone. The reason I do this is to disregard the change to the sheets' names because people do this. I did think of changing the codenames but don't know how. Where do I click on the editor to do that? I did some google search but no luck.
 
Upvote 0
Thanks everyone. The reason I do this is to disregard the change to the sheets' names because people do this. I did think of changing the codenames but don't know how. Where do I click on the editor to do that? I did some google search but no luck.
As far as I know there is no way to do that if I understood what you were trying to do.

The sheet index is determined by the sheet's postition in the tab order from left to right. If the person changed the name but not change the tab order, then referring to index is still valid. However, if someone change to sheet position, the macro will not valid anymore.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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