What's wrong with my VBA code to rename sheets?

ORoxo

Board Regular
Joined
Oct 30, 2016
Messages
149
Can any of you help me figuring out what's wrong with the code below?

Sub Macro()
Dim w As Worksheet
For Each w In Worksheets


If w.Name = "indkey" Or _
w.Name = "subdivkey" Or _
w.Name = "Q2" Or _
w.Name = "Q5" Or _
w.Name = "data_ps3" Then




Else


Worksheet.Name = Range("Z2")


End If


Next


End Sub

Thanks
 

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).
Try
Code:
w.Name = w.Range("Z2")
 
Upvote 0
the worksheet object you are using 'Worksheet' does not exist it appears. Perhaps you meant what Fluff said?

Also Im curious, are you working on something cfw related for ps3?
 
Upvote 0
You were right, stupid mistake

Now it's giving me this error:

wKT613Y.png


Code as follows:

Sub Macro()Dim w As Worksheet


If w.Name = "indkey" Or _
w.Name = "subdivkey" Or _
w.Name = "Q2" Or _
w.Name = "Q5" Or _
w.Name = "data_ps3" Then




Else


For Each w In Worksheets


w.Name = Range("Z2")


Next


End If


End Sub
 
Upvote 0
the worksheet object you are using 'Worksheet' does not exist it appears. Perhaps you meant what Fluff said?

Also Im curious, are you working on something cfw related for ps3?

No, not at all. Sorry to disappoint :p
 
Upvote 0
you should reference the specific worksheet that Z2 is on, maybe the active worksheet is something else?? You have to put a breakpoint in before that line and check the values of variables, etc
 
Upvote 0
It should be
Code:
Sub Macro()
   Dim w As Worksheet
   
   For Each w In Worksheets
      If w.Name = "indkey" Or _
         w.Name = "subdivkey" Or _
         w.Name = "Q2" Or _
         w.Name = "Q5" Or _
         w.Name = "data_ps3" Then
      Else
         w.Name = w.Range("Z2")
      End If
   Next
End Sub
 
Upvote 0
I just used to have a jailbroken ps3, and those string values seemed familiar. From the file structure of games or something. I was just wondering.
 
Upvote 0
It should be
Code:
Sub Macro()
   Dim w As Worksheet
   
   For Each w In Worksheets
      If w.Name = "indkey" Or _
         w.Name = "subdivkey" Or _
         w.Name = "Q2" Or _
         w.Name = "Q5" Or _
         w.Name = "data_ps3" Then
      Else
         w.Name = w.Range("Z2")
      End If
   Next
End Sub

That worked perfecly, Fluff

Understood what I was doing wrong, thanks!
 
Upvote 0

Forum statistics

Threads
1,224,889
Messages
6,181,611
Members
453,056
Latest member
apmale77

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