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.
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

gingersteve

Board Regular
Joined
Apr 19, 2007
Messages
169
In the worksheet's code module:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Name = Range("A1")

End Sub
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
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.
 

chumley

New Member
Joined
Sep 1, 2005
Messages
36
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.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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.
 

chumley

New Member
Joined
Sep 1, 2005
Messages
36
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.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
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>
 

Watch MrExcel Video

Forum statistics

Threads
1,122,841
Messages
5,598,390
Members
414,234
Latest member
grlevesq

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