Loop and Rename Worksheets

Joey_Ng

New Member
Joined
Mar 7, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to loop through a number of worksheets in the same workbook, look for the sheet names starting with sheet (eg, sheet1, sheet 2 etc...) and change those sheets with the value in cell B1 less first 4 characters. These sheets are actually "filtered pages" from a pivot table and each sheet has a value in B1. The code below only loop through the same activesheet but not actually doing anything. Help please.

Sub Loop_Rename()
Dim ws As Worksheets
Dim ct As Long, cr As String
cr = ActiveSheet.Range("B1").Value
ct = Len(ActiveSheet.Range("B1").Value) - 4
On Error Resume Next
For Each ws In ActiveWorkbook
If ws.name = "Sheet*" Then
ws.name = right(cr, ct).Value
Else
End If
Next ws
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The code below only loop through the same activesheet but not actually doing anything. Help please.

If you comment out the On Error Resume Next line the debugger will report all the errors you have in your code

Dave
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Loop_Rename()
   Dim ws As Worksheets
   Dim ct As Long, cr As String
   
   For Each ws In ActiveWorkbook
      If ws.Name = "Sheet*" Then
         cr = ws.Range("B1").Value
         ct = Len(ws.Range("B1").Value) - 4
         ws.Name = Right(cr, ct).Value
      End If
   Next ws
End Sub
 
Upvote 0
If you comment out the On Error Resume Next line the debugger will report all the errors you have in your code

Dave
Thanks Dave. It came up with "compile error: Wrong number of arguments or invalid property assignment"
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Loop_Rename()
   Dim ws As Worksheets
   Dim ct As Long, cr As String
  
   For Each ws In ActiveWorkbook
      If ws.Name = "Sheet*" Then
         cr = ws.Range("B1").Value
         ct = Len(ws.Range("B1").Value) - 4
         ws.Name = Right(cr, ct).Value
      End If
   Next ws
End Sub
Thanks Fluff. This one also came up with the same "compile error: Wrong number of arguments or invalid property assignment" as per original code for "ws.Name=Right(cr,ct.Value). Any idea what this refers to?
 
Upvote 0
Thanks Fluff. This one also came up with the same "compile error: Wrong number of arguments or invalid property assignment" as per original code for "ws.Name=Right(cr,ct.Value). Any idea what this refers to?
sorry... I meant ws.Name = right(cr, ct).Value with the word "right" highlighted
 
Upvote 0
Thanks Dave. It came up with "compile error: Wrong number of arguments or invalid property assignment"


Rich (BB code):
Sub Loop_Rename()
    Dim ws As Worksheet
    Dim ct As Long, cr As String
   
    cr = ActiveSheet.Range("B1").Value
    ct = Len(ActiveSheet.Range("B1").Value) - 4
   
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "Sheet*" Then
            ws.Name = Right(cr, ct)
        Else
        End If
    Next ws
End Sub

Just looking at your code make the changes I have shown in BOLD & see if resolves

Dave
 
Upvote 0
Rich (BB code):
Sub Loop_Rename()
    Dim ws As Worksheet
    Dim ct As Long, cr As String
  
    cr = ActiveSheet.Range("B1").Value
    ct = Len(ActiveSheet.Range("B1").Value) - 4
  
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "Sheet*" Then
            ws.Name = Right(cr, ct)
        Else
        End If
    Next ws
End Sub

Just looking at your code make the changes I have shown in BOLD & see if resolves

Dave
Hi Dave,

Much appreciated for your help. Your code only successful on the first sheet then error "name already taken". Image also shows a sample of my data and what I am trying to do.
 

Attachments

  • Loop Image.png
    Loop Image.png
    143.5 KB · Views: 9
Upvote 0
If you are getting sheet name from EACH sheet cell B1 then follow Fluffs suggestion by placing that part of the code in the For Next Loop
or you will have to put and exit sub in the If Statement

Rich (BB code):
For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "Sheet*" Then
            ws.Name = Right(cr, ct)
            Exit Sub
        Else
        End If

Dave
 
Upvote 0
If you are getting sheet name from EACH sheet cell B1 then follow Fluffs suggestion by placing that part of the code in the For Next Loop
or you will have to put and exit sub in the If Statement

Rich (BB code):
For Each ws In ActiveWorkbook.Worksheets
        If ws.Name Like "Sheet*" Then
            ws.Name = Right(cr, ct)
            Exit Sub
        Else
        End If

Dave
Thanks for your time. Finally got a working code:
Sub Rename()
Dim ws As Worksheet
Dim ct As Long, cr As String

For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "Sheet*" Then
cr = ws.Range("B1").Value
ct = Len(ws.Range("B1").Value) - 4
ws.Name = Right(cr, ct)
Else
End If
Next ws
End Sub
Appreciate ALL for your assistance. Thank You.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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