Renaming tabs "400" macro run error

Aeafa

New Member
Joined
Aug 31, 2016
Messages
12
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
Hello!

I am having trouble running a macro that was written to re-name the tabs in a worksheet.
It comes up with a box and "400".

Now my main source of confusion is that I have the exact same code & references in another spreadsheet and it works 100% fine.
Does anyone know what could be causing this to stop working in this particular spreadsheet?

Many many thanks in advance.

Oh, and this is the code I am using.

Sub RenameTabs()
For x = 1 To Sheets.Count
If Worksheets(x).Range("A4").Value <> "" Then
Sheets(x).Name = Worksheets(x).Range("A4").Value
End If
Next
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Maybe one of the values in the Range("A4").
Are duplicates or improper values for a sheet name
 
Upvote 0
Solution
If you adding some error handling, you can get it to tell you which sheet/value is giving your problems, i.e.
Code:
Sub RenameTabs()

    Dim x As Long
    Dim myName

    On Error GoTo err_chk
    For x = 1 To Sheets.Count
        myName = Worksheets(x).Range("A4").Value
        If myName <> "" Then
            Sheets(x).Name = myName
        End If
    Next
    On Error GoTo 0
    
    Exit Sub
    

err_chk:
    If Err.Number = 400 Then
        MsgBox "Error on sheet " & x & " when name is " & myName
    Else
        MsgBox Err.Number & ":" & Err.Description
    End If
    
End Sub
 
Upvote 0
Thank you both so so so much! I didn't even think of that! -facepalm-

Turns out I was trying to rename a tab to the same name that one of them already was.
I can't believe I missed that... -shakes head in disgrace-

Again, thank you so very much, ya'll really have no idea how much I appreciate this!! :)
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
Thank you both so so so much! I didn't even think of that! -facepalm-

Turns out I was trying to rename a tab to the same name that one of them already was.
I can't believe I missed that... -shakes head in disgrace-

Again, thank you so very much, ya'll really have no idea how much I appreciate this!! :)
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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