Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Worksheet name

2 views
Skip to first unread message

walrus

unread,
Jan 8, 2010, 3:41:01 AM1/8/10
to
Hi

What should i do so that the Worksheet (Tab) name is the same as what i type
in a specific cell?

Regards,

trip_to_tokyo

unread,
Jan 8, 2010, 3:53:01 AM1/8/10
to
Hi Walrus. I think that the only thing to do is to type them in separately.

I am not aware that you can pull one from the other automatically.

Please hit Yes if my comments have helped.

Thanks.

Mike H

unread,
Jan 8, 2010, 4:07:02 AM1/8/10
to
Hi,

Right click your sheet tab, view code and paste the code in. Change A1 to
the cell you want

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
On Error Resume Next
Application.EnableEvents = False
ActiveSheet.Name = Target.Value
Application.EnableEvents = True
End If
End Sub

Mike

Jacob Skaria

unread,
Jan 8, 2010, 4:18:02 AM1/8/10
to

Just to add on to what Mike has posted.

If you are looking at naming all tabs in a workbook use the workbook level
Sheet Change event. Incase you are new to VBA set the security level to
low/medium in (Tools|Macro|Security). From workbook press Alt+F11 to launch
VBE (Visual Basic Editor). From the left treeview search for the workbook
name and click on + to expand it. Within that you should see the following

VBAProject(Your_Filename)
Microsoft Excel Objects
Sheet1(Sheet1)
Sheet2(Sheet2)
Sheet3(Sheet3)
This Workbook

Double click 'This WorkBook' and paste the below code to the right code pane.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Address = "$A$1" And Target.Text <> "" Then


On Error Resume Next
Application.EnableEvents = False

Sh.Name = Target.Text


Application.EnableEvents = True
End If
End Sub

--
Jacob

Mike H

unread,
Jan 8, 2010, 4:29:01 AM1/8/10
to
Walrus

That code will get you into trouble because you aren't trapping for illegal
file names
Include the error trap I posted at the top of your code

On Error resume next

Mike

"walrus" wrote:

> Thanks guys...i found and used the following VBA Function and it worked
> perfectly.
>
> Sub name_um()
> For Each ws In Worksheets
> ws.Name = ws.Range("D1").Value
> Next
> End Sub

walrus

unread,
Jan 8, 2010, 4:24:01 AM1/8/10
to
Thanks guys...i found and used the following VBA Function and it worked
perfectly.

Sub name_um()
For Each ws In Worksheets
ws.Name = ws.Range("D1").Value
Next
End Sub

walrus

unread,
Jan 8, 2010, 4:49:01 AM1/8/10
to
Mike

Where should i include this "On Error resume next"??

Mike H

unread,
Jan 8, 2010, 4:52:04 AM1/8/10
to
Hi,

Like this

Sub name_um()
On Error Resume Next


For Each ws In Worksheets
ws.Name = ws.Range("D1").Value
Next
End Sub

Mike

Jacob Skaria

unread,
Jan 8, 2010, 4:56:01 AM1/8/10
to
Walrus, the sheet event will change the sheet name as soon as the cell value
is changed. The macro which you posted needs to be run separately to change
the tab names

--
Jacob

walrus

unread,
Jan 8, 2010, 5:49:01 AM1/8/10
to
Mike/Jacob

Thanks. This works even better as i don't have to worry about older tabs
being renamed.

Regards,

Bob Phillips

unread,
Jan 8, 2010, 7:26:25 AM1/8/10
to
Nor did yours trap it, it just ignored it. Better to handle it IMO

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then
Application.EnableEvents = False
ActiveSheet.Name = ValidName(Target.Value)


Application.EnableEvents = True
End If
End Sub

Function ValidName(ByVal TheFileName As String) As String
Dim RegEx As Object
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.Pattern = "[\\/:\*\?""<>\|]"
ValidName = RegEx.Replace(TheFileName, "")
Set RegEx = Nothing
End Function

---
HTH

Bob Phillips

"Mike H" <Mi...@discussions.microsoft.com> wrote in message
news:4B6042BF-86A0-4021...@microsoft.com...

0 new messages