Run-Time error '1004'

Carin

Board Regular
Joined
Feb 4, 2006
Messages
224
I need to rename worksheets based on cell A1 and found this code in this forum from 2004. I'm currently using 2010. The macro works great but generates a Run-Time error at the end. Does anyone know a workaround?


'Sub Rep()
For Each sht In ThisWorkbook.Worksheets
sht.Name = sht.Range("A1")
Next sht
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Do all your sheets get the names you have in A1 of each sheet, including any hidden sheets? If not, one or more sheets may have characters (like "/") in A1 that are not allowed for sheet names.
 
Upvote 0
Try this
Code:
Sub Rep()
     For Each sht In ThisWorkbook.Worksheets
         If sht.Range("A1") <> "" Then
             sht.Name = sht.Range("A1")
        End If
    Next sht
 End Sub

You need data in Cell A1 for the sheets you want to change the name on.
 
Last edited:
Upvote 0
This works perfect - @JoeMoe - there were no characters, I actually had blank worksheets.
@JLGWhiz - thank you. I didn't consider having blank worksheets causing a problem.
 
Upvote 0
This works perfect - @JoeMoe - there were no characters, I actually had blank worksheets.
@JLGWhiz - thank you. I didn't consider having blank worksheets causing a problem.
they wouldn't be if you were addressing one sheet to make the name change, but your code addresses each sheet in the workbook, so if any of the sheets have no data in cell A1, you get the error message.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,645
Members
448,974
Latest member
DumbFinanceBro

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