PC Review


Reply
Thread Tools Rate Thread

Change sheet names based on cell contents

 
 
Jim G
Guest
Posts: n/a
 
      19th Jun 2009
I have the following code that changes the sheet name based on the entry in
cell A1. This allows the user to create multiple sheet copies of a template
then updateing the data in each without needing to update sheet names as they
go.

Sub UpdateTabName()
Dim Ws As Worksheet
‘ the macro will rename all sheets in the active workbook to the contents of
A1

With ActiveWorkbook

For Each Ws In Worksheets
Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
Next Ws

End With

End Sub

I would like to insert a error trap and message for when the target cell is
blank or has illegal characters. Ideally, I would like to provide a means
for the user to be prompted for a new name for that sheet. If that is not
possible or practical, insert a temporary name and alert the user to change
it.

Any suggestions?

--
Jim
 
Reply With Quote
 
 
 
 
Barb Reinhardt
Guest
Posts: n/a
 
      19th Jun 2009
You could try something like this

For Each Ws In .Worksheets
On Error Resume next
Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
If Err <> 0 then
WS.Select
on error resume next
WS.Name = InputBox("Enter Worksheet Name",WSName)
if err <> 0 then Msgbox ("Unable to set worksheet name.")
end if
on error goto 0
Next Ws

"Jim G" wrote:

> I have the following code that changes the sheet name based on the entry in
> cell A1. This allows the user to create multiple sheet copies of a template
> then updateing the data in each without needing to update sheet names as they
> go.
>
> Sub UpdateTabName()
> Dim Ws As Worksheet
> ‘ the macro will rename all sheets in the active workbook to the contents of
> A1
>
> With ActiveWorkbook
>
> For Each Ws In Worksheets
> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
> Next Ws
>
> End With
>
> End Sub
>
> I would like to insert a error trap and message for when the target cell is
> blank or has illegal characters. Ideally, I would like to provide a means
> for the user to be prompted for a new name for that sheet. If that is not
> possible or practical, insert a temporary name and alert the user to change
> it.
>
> Any suggestions?
>
> --
> Jim

 
Reply With Quote
 
keiji kounoike
Guest
Posts: n/a
 
      19th Jun 2009
Try this one.

Sub UpdateTabName()
Dim Ws As Worksheet
Dim n

With ActiveWorkbook
For Each Ws In Worksheets
On Error GoTo ex:
Ws.Select
Ws.Name = Ws.Range("A1").Text
Ignore:
Next Ws

End With
Exit Sub
ex:
n = Application.InputBox("Wrong Sheet'S Name!!", _
Default:=Ws.Range("A1").Text, Type:=2)
If VarType(n) = vbBoolean Then
Resume Ignore:
End If
Ws.Range("A1") = n
Resume
End Sub

Keiji

Barb Reinhardt wrote:
> You could try something like this
>
> For Each Ws In .Worksheets
> On Error Resume next
> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
> If Err <> 0 then
> WS.Select
> on error resume next
> WS.Name = InputBox("Enter Worksheet Name",WSName)
> if err <> 0 then Msgbox ("Unable to set worksheet name.")
> end if
> on error goto 0
> Next Ws
>
> "Jim G" wrote:
>
>> I have the following code that changes the sheet name based on the entry in
>> cell A1. This allows the user to create multiple sheet copies of a template
>> then updateing the data in each without needing to update sheet names as they
>> go.
>>
>> Sub UpdateTabName()
>> Dim Ws As Worksheet
>> ‘ the macro will rename all sheets in the active workbook to the contents of
>> A1
>>
>> With ActiveWorkbook
>>
>> For Each Ws In Worksheets
>> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
>> Next Ws
>>
>> End With
>>
>> End Sub
>>
>> I would like to insert a error trap and message for when the target cell is
>> blank or has illegal characters. Ideally, I would like to provide a means
>> for the user to be prompted for a new name for that sheet. If that is not
>> possible or practical, insert a temporary name and alert the user to change
>> it.
>>
>> Any suggestions?
>>
>> --
>> Jim

 
Reply With Quote
 
keiji kounoike
Guest
Posts: n/a
 
      19th Jun 2009
Sorry Barb for wrong post.
This was intended to Jim's post, Message Id:
22B8A7A7-0541-4446-8A20-(E-Mail Removed)>

Keiji

keiji kounoike wrote:
> Try this one.
>
> Sub UpdateTabName()
> Dim Ws As Worksheet
> Dim n
>
> With ActiveWorkbook
> For Each Ws In Worksheets
> On Error GoTo ex:
> Ws.Select
> Ws.Name = Ws.Range("A1").Text
> Ignore:
> Next Ws
>
> End With
> Exit Sub
> ex:
> n = Application.InputBox("Wrong Sheet'S Name!!", _
> Default:=Ws.Range("A1").Text, Type:=2)
> If VarType(n) = vbBoolean Then
> Resume Ignore:
> End If
> Ws.Range("A1") = n
> Resume
> End Sub
>
> Keiji
>
> Barb Reinhardt wrote:
>> You could try something like this
>>
>> For Each Ws In .Worksheets
>> On Error Resume next
>> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
>> If Err <> 0 then
>> WS.Select
>> on error resume next
>> WS.Name = InputBox("Enter Worksheet Name",WSName)
>> if err <> 0 then Msgbox ("Unable to set worksheet name.")
>> end if
>> on error goto 0 Next Ws
>> "Jim G" wrote:
>>
>>> I have the following code that changes the sheet name based on the
>>> entry in cell A1. This allows the user to create multiple sheet
>>> copies of a template then updateing the data in each without needing
>>> to update sheet names as they go.
>>>
>>> Sub UpdateTabName()
>>> Dim Ws As Worksheet
>>> ‘ the macro will rename all sheets in the active workbook to the
>>> contents of A1
>>>
>>> With ActiveWorkbook
>>> For Each Ws In Worksheets
>>> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
>>> Next Ws
>>>
>>> End With
>>>
>>> End Sub
>>>
>>> I would like to insert a error trap and message for when the target
>>> cell is blank or has illegal characters. Ideally, I would like to
>>> provide a means for the user to be prompted for a new name for that
>>> sheet. If that is not possible or practical, insert a temporary name
>>> and alert the user to change it.
>>>
>>> Any suggestions?
>>>
>>> --
>>> Jim

 
Reply With Quote
 
Jim G
Guest
Posts: n/a
 
      19th Jun 2009
Thanks Barb, worked great. I was going to ask for the cell reference to be
updated with the new name, but Keiji beat me to it.

Cheers
--
Jim


"Barb Reinhardt" wrote:

> You could try something like this
>
> For Each Ws In .Worksheets
> On Error Resume next
> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
> If Err <> 0 then
> WS.Select
> on error resume next
> WS.Name = InputBox("Enter Worksheet Name",WSName)
> if err <> 0 then Msgbox ("Unable to set worksheet name.")
> end if
> on error goto 0
> Next Ws
>
> "Jim G" wrote:
>
> > I have the following code that changes the sheet name based on the entry in
> > cell A1. This allows the user to create multiple sheet copies of a template
> > then updateing the data in each without needing to update sheet names as they
> > go.
> >
> > Sub UpdateTabName()
> > Dim Ws As Worksheet
> > ‘ the macro will rename all sheets in the active workbook to the contents of
> > A1
> >
> > With ActiveWorkbook
> >
> > For Each Ws In Worksheets
> > Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
> > Next Ws
> >
> > End With
> >
> > End Sub
> >
> > I would like to insert a error trap and message for when the target cell is
> > blank or has illegal characters. Ideally, I would like to provide a means
> > for the user to be prompted for a new name for that sheet. If that is not
> > possible or practical, insert a temporary name and alert the user to change
> > it.
> >
> > Any suggestions?
> >
> > --
> > Jim

 
Reply With Quote
 
Jim G
Guest
Posts: n/a
 
      19th Jun 2009
Brilliant! You anticipated my every need.

Thanks Heaps.

Cheers
--
Jim


"keiji kounoike" <"kounoike AT mbh.nifty." wrote:

> Try this one.
>
> Sub UpdateTabName()
> Dim Ws As Worksheet
> Dim n
>
> With ActiveWorkbook
> For Each Ws In Worksheets
> On Error GoTo ex:
> Ws.Select
> Ws.Name = Ws.Range("A1").Text
> Ignore:
> Next Ws
>
> End With
> Exit Sub
> ex:
> n = Application.InputBox("Wrong Sheet'S Name!!", _
> Default:=Ws.Range("A1").Text, Type:=2)
> If VarType(n) = vbBoolean Then
> Resume Ignore:
> End If
> Ws.Range("A1") = n
> Resume
> End Sub
>
> Keiji
>
> Barb Reinhardt wrote:
> > You could try something like this
> >
> > For Each Ws In .Worksheets
> > On Error Resume next
> > Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
> > If Err <> 0 then
> > WS.Select
> > on error resume next
> > WS.Name = InputBox("Enter Worksheet Name",WSName)
> > if err <> 0 then Msgbox ("Unable to set worksheet name.")
> > end if
> > on error goto 0
> > Next Ws
> >
> > "Jim G" wrote:
> >
> >> I have the following code that changes the sheet name based on the entry in
> >> cell A1. This allows the user to create multiple sheet copies of a template
> >> then updateing the data in each without needing to update sheet names as they
> >> go.
> >>
> >> Sub UpdateTabName()
> >> Dim Ws As Worksheet
> >> ‘ the macro will rename all sheets in the active workbook to the contents of
> >> A1
> >>
> >> With ActiveWorkbook
> >>
> >> For Each Ws In Worksheets
> >> Ws.Name = Ws.Range("A1") ‘ change to the cell address to be used
> >> Next Ws
> >>
> >> End With
> >>
> >> End Sub
> >>
> >> I would like to insert a error trap and message for when the target cell is
> >> blank or has illegal characters. Ideally, I would like to provide a means
> >> for the user to be prompted for a new name for that sheet. If that is not
> >> possible or practical, insert a temporary name and alert the user to change
> >> it.
> >>
> >> Any suggestions?
> >>
> >> --
> >> Jim

>

 
Reply With Quote
 
keiji kounoike
Guest
Posts: n/a
 
      19th Jun 2009
You're welcome.

Keiji

Jim G wrote:
> Brilliant! You anticipated my every need.
>
> Thanks Heaps.
>
> Cheers

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Based new sheet names on cell value J.W. Aldridge Microsoft Excel Programming 0 24th Jun 2009 01:59 PM
Change contents of a cell based on cell contents. =?Utf-8?B?TWFobmlhbg==?= Microsoft Excel Programming 3 4th May 2007 10:49 PM
Macro to Clear Cell Contents based on Cell Value in another Sheet CH Microsoft Excel Discussion 4 26th Jan 2007 08:37 PM
Lookup cell contents in on sheet based on a formula in second sheet Michael Wright via OfficeKB.com Microsoft Excel Worksheet Functions 1 30th Apr 2005 04:11 PM
Please help! Macro to change cell contents based on cell to the left Jennifer Microsoft Excel Programming 7 4th Mar 2004 01:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:58 AM.