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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,067
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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

Carin

Board Regular
Joined
Feb 4, 2006
Messages
224
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

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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,190,808
Messages
5,983,031
Members
439,815
Latest member
yoswosz

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
Top