Worksheet re-naming

chumley

New Member
Joined
Sep 1, 2005
Messages
36
Hi all,

I have some text in cell A1 of a worksheet, that changes on a daily basis. I would like to automatically change the name of the worksheet that this cell is contained in, to match the text in the cell.

Can anyone help out with the code to do this?

Thanks in advance for any help.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
In the worksheet's code module:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Name = Range("A1")

End Sub
 
Upvote 0
chumley

If the text in A1 is the result of a formula, you will need to use the Worksheet_Calculate event (rather than Worksheet_Change) to trigger the code.

Post back if you need help with how to implement the code or if these suggestions didn't satisfy your requirements.
 
Upvote 0
Thanks for your help both.

Being a bit of an Excel novice, I'm not sure where to put this code. I have an existing macro that creates a new worksheet within an existing workbook, it is after this has been run that I would like to change the name of the worksheet.
 
Upvote 0
I have some text in cell A1 of a worksheet, that changes on a daily basis. I would like to automatically change the name of the worksheet that this cell is contained in, to match the text in the cell
I have an existing macro that creates a new worksheet within an existing workbook, it is after this has been run that I would like to change the name of the worksheet.
These two statements seem somewhat contradictory to me. Can you clarify just what is happening?

If it is not too long and complicated, your existing code and an explanation of what it is doing might also be worth posting on the board.
 
Upvote 0
Peter,

Here is the code that I am using:

Sub TestMacro()
ActiveSheet.Copy Before:=Sheets(1)
Range("S9:S11").Select
Selection.Copy
Range("D8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("I8:I10").Select
Selection.ClearContents
End Sub

As you can see it's very simple, it creates a new worksheet and then performs some re-formatting. I would then like the macro to go on and rename the new worksheet that has been created calling it the same as the text that is contained within cell A1.

I hope this helps, apologies if I haven't been clear. Thanks again for your help.
 
Upvote 0
chumley

Try the code below. As well as naming the sheet, the "On Error ... " line stops you getting an error message if you run the macro more than once with the same value in A1 or if the value in A1 is an illegal sheet name.

Also, in code, you generally do not need to actually select cells to work with them and selecting slows the code considerably. So I have suggested some changes to the rest of your code as well to speed it up. You won't notice the difference in this short bit of code, but good knowledge for the future.

"Application.ScreenUpdating ..." lines stop some flickering while the code is being run and again speeds the code up.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> TestMacro()
    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
    ActiveSheet.Copy Before:=Sheets(1)
    
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    ActiveSheet.Name = Range("A1").Value
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0

    Range("S9:S11").Copy
    Range("D8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("I8:I10").ClearContents
    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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