Case "Question"

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Good Evening,

I have a Case code question.

In having:

Code:
         Case "Tab Name"

Now the
"Tab Name"
represents just that, the name at the bottom of the tab of which is the actual name of the worksheet.

Is there anyway to specify this in a generic fashion?

For example with the sheet name?

For example, Sheet1 (Tab Name); somehow using Sheet1 as the name for a Case.

I don't know how to code this.

Please help.

Thank you,
Pinaeous
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Something like this ??

Code:
For Each ws In Worksheets
 ws.Activate
    Select Case (ws.Name)
           Case "Sheet1"
                'Do Stuff

            Case "Sheet2"
                'Do other stuff
           Case Else
                "Do more stuff
     End Select
 
Upvote 0
Hi Michael,

Be patient with me if I confuse it,

In the VBA window, under Microsoft Excel Objects, it lists ..
Sheet1(Name1) - I've named the tab as Name1

So, here the worksheet, ws.Name = Name1 but now if I want with,

Case "Sheet1" meaning the worksheet(1) is this the same as your answer??
 
Last edited:
Upvote 0
Be patient with me if I confuse it,
you confused me...:lol:


Code:
For Each ws In Worksheets
 ws.Activate
    Select Case (ws.Name)[color=red]'this doesn't change !![/color]
           Case "Name1" [color=red]'this changes to your required sheet name !![/color]
                'Do Stuff

            Case "YOURSHEETNAME"[color=red]'this changes to your required sheet name !![/color]
                'Do other stuff
           Case Else
                "Do more stuff
     End Select

Did that help ??
 
Upvote 0
Code:
        Select Case UCase(ws.Name)
        
            Case "Sheet1"

What happens if you want …

Code:
        Select Case UCase(ws.NUMBER)
        
            Case Worksheets(2)

So, not in using the ws.Name but using its ws.Number.

How do I do this?

& Thank you!
 
Upvote 0
Ahh...you mean like this ??

Code:
Sub MM1()
Dim ws As Worksheet
For Each ws In Worksheets
 ws.Activate
    Select Case ActiveSheet.Index
           Case 1 "sheet number
                'Do stuff
            Case 2 'sheet number
               'Do more stuff
     End Select
Next ws
End Sub
 
Upvote 0
Hi Michael,

I think that did it for my question.

Thank you,
Paul
 
Upvote 0
Keeping in mind of course that you may / may not need to loop through the sheets.
So, if you want to apply / or not, the code to activesheets it could simply be

Code:
Sub MM1()
Select Case ActiveSheet.Index
   Case 1 "sheet number
           'Do stuff
   Case 2 'sheet number
           'Do more stuff
End Select
End Sub
 
Last edited:
Upvote 0
In the VBA window, under Microsoft Excel Objects, it lists ..
Sheet1(Name1) - I've named the tab as Name1

So, here the worksheet, ws.Name = Name1 but now if I want with,

Case "Sheet1" meaning the worksheet(1)
Just a word of warning, Sheet1 is NOT the same as worksheet(1).
Sheet1 is the codename of the sheet & will appear in the Project window in alphabetical order.
Worksheets(1) is the sheet index & refers to the position of sheet in the workbook. So the left most sheet is sheet(1), the next is sheet(2) etc.
Using the index number is unreliable, because if somebody changes the order of the sheets, your code will potentially still work, but on the wrong sheet.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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