Share via

Getting error

Anonymous
2023-06-10T12:48:33+00:00

Getting "run-time error '1004': Mathod 'name of object'_Worksheet' failed "

Sub Change_name()

Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets

sh.name = Left(sh.Range("A5").Value, 3)

Next sh

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2023-06-10T16:17:18+00:00

    Hi Narayan,

    try it this way:

    Sub Change_name() 
    
    Dim i As Integer 
    
    With ThisWorkbook 
    
          For i = 1 To .Worksheets.Count 
    
                .Worksheets(i).Name = Left(Sheets(i).Range("A5"), 3) 
    
          Next 
    
    End With 
    
    End Sub
    

    Claus

    0 comments No comments
  2. Anonymous
    2023-06-10T16:00:52+00:00

    its showing

    Run-time error '424' Object required

    0 comments No comments
  3. HansV 462.6K Reputation points MVP Volunteer Moderator
    2023-06-10T13:59:57+00:00

    When the error occurs, activate the Immediate window (Ctrl+G).

    Type or copy/paste the following:

    ? Left(sh.Range("A1").Value, 3)

    and press Enter. What is the result?

    0 comments No comments
  4. Anonymous
    2023-06-10T13:38:48+00:00

    debug is getting on sh.name = Left(sh.Range("A1").Value, 3)

    Perhaps one of the values would result in a name that is not allowed, or in a duplicate name.

    • A worksheet name cannot contain any of the following characters: / \ ? * : [ ]
    • A worksheet name cannot begin or end with an apostrophe '
    • A worksheet name must be unique within the workbook

    I have checked the above-mentioned criteria, and there is no issue with it

    0 comments No comments
  5. HansV 462.6K Reputation points MVP Volunteer Moderator
    2023-06-10T13:05:12+00:00

    Perhaps one of the values would result in a name that is not allowed, or in a duplicate name.

    • A worksheet name cannot contain any of the following characters: / \ ? * : [ ]
    • A worksheet name cannot begin or end with an apostrophe '
    • A worksheet name must be unique within the workbook
    0 comments No comments