Exclude specific sheets from loop code

Javi

Active Member
Joined
May 26, 2011
Messages
440
Hi All,

I'm looking for some assistance with modifying this code.

I would like to exclude a few sheets from the loop. Specifically the sheets code "Sheet1" "Sheet7" and "Sheet8" this is the code identifier not sheet name.

Any help or direction would be appreciated thanks.

VBA Code:
Sub tabname()
Dim ws As Worksheet
For Each ws In Worksheets
    On Error Resume Next
    If Len(ws.Range("B17")) > 0 Then
        ws.Name = ws.Range("B17").Value
    End If
    On Error GoTo 0
    If ws.Name <> ws.Range("B17").Value Then
        MsgBox ws.Name & " Was Not renamed, the suggested name was invalid"
    End If
Next
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,

try this:

Rich (BB code):
Sub tabname()
Dim ws As Worksheet
For Each ws In Worksheets
    On Error Resume Next
    If ws.Name <> "Sheet3" and ws.Name <> "Sheet5" then
    If Len(ws.Range("B17")) > 0 Then
        ws.Name = ws.Range("B17").Value
    End If
    End If
    On Error GoTo 0
    If ws.Name <> ws.Range("B17").Value Then
        MsgBox ws.Name & " Was Not renamed, the suggested name was invalid"
    End If
Next
End Sub
 
Upvote 0
.. exclude a few sheets from the loop. Specifically the sheets code "Sheet1" "Sheet7" and "Sheet8" this is the code identifier not sheet name.

See if this is it.
VBA Code:
Sub tabname()
  Dim ws As Worksheet

  For Each ws In Worksheets
    Select Case ws.CodeName
      Case "Sheet1", "Sheet7", "Sheet8"
      Case Else
        On Error Resume Next
        ws.Name = ws.Range("B17").Value
        On Error GoTo 0
        If ws.Name <> ws.Range("B17").Value Then MsgBox ws.CodeName & " (" & ws.Name & ") Was Not renamed, the suggested name (" & ws.Range("B17").Value & ") was invalid"
    End Select
  Next ws
End Sub

Note that as well as addressing the exclusion of certain sheets, I have also removed the test ..
VBA Code:
If Len(ws.Range("B17")) > 0 Then
.. since you cannot rename a sheet to a zero length string anyway.
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,527
Members
449,037
Latest member
tmmotairi

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