VBA Run-time error 13 - Renaming Worksheets from range

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Code:
Dim ws As Worksheets

For Each ws In Worksheets
   For z = 2 to 26
     [COLOR=#ff0000]If ws.Name <> "Template" And "Rota" Then[/COLOR]
       ws.Name = Cells(1, z)
     End If
   Next
Next

The red is the offending line. All I'm trying to do is rename each worksheet after each cell in B1:Z1 (There is the same no. Of corresponding worksheets). Any ideas what I'm missing?

Thank you in advance for your help
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about
Code:
Sub BenGee()
   Dim Ws As Worksheet
   Dim i As Long
   
   i = 2
   For Each Ws In Worksheets
      If Ws.Name <> "Template" And Ws.Name <> "Rota" Then
         Ws.Name = Sheets("[COLOR=#ff0000]Rota[/COLOR]").Cells(1, i).Value
         i = i + 1
      End If
   Next Ws
End Sub
Change value in red to match the sheet with the new sheet names.
 
Upvote 0
Thank you both. Seems fairly obvious now. Appreciate your help.

Fluff, you truly are a gem. Thank you
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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