Quick and Easy VBA Fix/Help?

GaryG9595

Board Regular
Joined
Jun 13, 2014
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,
Currently I have a VBA Code that I use to change the names of all the tabs to a specific cell in the worksheet. See VBA Code 1. below. In this case it's "A2". I obvioulsy can change that cell to point other cells for future changes.
My question is, how can this code be modified to exclude renaming certain sheets?

I have another code the splits the sheets into their own file folder excluding the "Master Data" and "Pricing" sheets.
I have tried to blend the codes to get it to work, and I can't figure it out. I am still learning.
This was part of the code that I tried.
If xWS.Name <> "Master Data" And xWS.Name <> "Pricing"

I need to rename all the sheets to cell "DO2" excluding these two sheets "Master Data" and "Pricing"

VBA Code 1.
Sub RenameTabs()
For i = 1 To Sheets.Count
If Worksheets(i).Range("A2").Value <> "" Then
Sheets(i).Name = Worksheets(i).Range("A2").Value
End If
Next
End Sub

Thanks in advance,
Gary
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
How about
VBA Code:
Sub RenameTabs()
   Dim Ws As Worksheet
   
   For Each Ws In Worksheets
      If Ws.Name <> "Master Data" And Ws.Name <> "Pricing" Then
         If Ws.Range("DO2").Value <> "" Then Ws.Name = Ws.Range("DO2").Value
      End If
   Next Ws
End Sub
 
Upvote 0
try this:
VBA Code:
Sub RenameTabs()
For i = 1 To Sheets.Count
If Worksheets(i).Name <> "Master Data" And Worksheets(i).Name <> "Pricing" Then
    If Worksheets(i).Range("A2").Value <> "" Then
    Sheets(i).Name = Worksheets(i).Range("A2").Value
    End If
End If
Next
End Sub
 
Upvote 0
Try this macro. Add any other sheet names you want to exclude to the array.
VBA Code:
Sub RenameTabs()
    Application.ScreenUpdating = False
    Dim arr As Variant, i As Long
    arr = Array("Master Data", "Pricing")
    For i = 1 To Sheets.Count
        If IsError(Application.Match(Sheets(i).Name, arr, 0)) Then
            If Worksheets(i).Range("A2").Value <> "" Then
                Sheets(i).Name = Worksheets(i).Range("A2").Value
            End If
        End If
    Next
    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Solution
Thank you all... You all are the best. :)
All of them worked and hopefully this will help others in the future as well.
I ended up using mumps suggestion as it was easier to add multiple sheets to exclude.
Thanks again for all your help.
Gary
 
Upvote 0
If you want to exclude multiple sheets, another option is
VBA Code:
Sub RenameTabs()
   Dim Ws As Worksheet
   
   For Each Ws In Worksheets
      Select Case Ws.Name
         Case "Master Data", "Pricing"
         Case Else
            If Ws.Range("DO2").Value <> "" Then Ws.Name = Ws.Range("DO2").Value
      End Select
   Next Ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

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