VBA Change Worksheet to Workbook Question

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

If I'm working with the following Sub that acts upon a specific sheet name its "TAB NAME", do you know how I can change this to a worksheet number?

VBA Code:
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

If ws.Name <> "Tab1 Name" Then
'Change "TAB NAME"

If ws.Name <> "Tab2 Name" And ws.Name <> "Tab1 Name" Then

ws.visible = xlSheetHidden

Application.ScreenUpdating = False

End If
Next

Thank you for your help!
pinaceous
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You can use ws.Index. However, if the user rearranges the order of the worksheets, the indices will change. Consider using the CodeName of the worksheet.
 
Upvote 0
Could you be a little more clear in your objective. The code is making a name comparison. Do you want to try to make an index number comparison? Please be specific about the problem you are having if it is a problem. If you simply wish to change the type of comparison you might have some difficulty as @dataluver points out.
 
Upvote 0
You can use ws.Index. However, if the user rearranges the order of the worksheets, the indices will change. Consider using the CodeName of the worksheet.

Hi dataluver,

I've locked the workbook, so the user will not be changing the worksheets around.

In following through, could you please provide me with an example in using the ws.index?

Thank you,
pinaceous
 
Upvote 0
Here is an example of using Index and CodeName. Even if the user is not able to rearrange worksheets, using the codename is still the less brittle choice IMO. If you later insert or delete a worksheet, it may change other worksheets' index.

VBA Code:
Sub UsingIndex()
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
    
        Select Case ws.Index
            Case 1, 2
                ws.Visible = xlSheetHidden
        End Select
        
    Next
End Sub

Sub UsingCodeName()
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
    
        Select Case ws.CodeName
            Case "Sheet1", "Sheet2"
                ws.Visible = xlSheetHidden
        End Select
        
    Next
End Sub
 
Upvote 0
Just to point out that sheet code name is not always in left to right order either, and can be changed by users who have access to the vb editor. But sheet code name is a bit safer than index for sure.
 
Upvote 0
Here is an example of using Index and CodeName. Even if the user is not able to rearrange worksheets, using the codename is still the less brittle choice IMO. If you later insert or delete a worksheet, it may change other worksheets' index.

VBA Code:
Sub UsingIndex()
    Dim ws As Worksheet
   
    For Each ws In ActiveWorkbook.Worksheets
   
        Select Case ws.Index
            Case 1, 2
                ws.Visible = xlSheetHidden
        End Select
       
    Next
End Sub

Sub UsingCodeName()
    Dim ws As Worksheet
   
    For Each ws In ActiveWorkbook.Worksheets
   
        Select Case ws.CodeName
            Case "Sheet1", "Sheet2"
                ws.Visible = xlSheetHidden
        End Select
       
    Next
End Sub

Thank you dataluver for sending the two codes! When words get in the way, code works wonders every time!

For my application, I'm going to use the
VBA Code:
Sub UsingIndex()
.

Thanks again!
pinaceous
 
Upvote 0

Forum statistics

Threads
1,214,917
Messages
6,122,233
Members
449,075
Latest member
staticfluids

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