Renaming sheet from a cell

tugsy

New Member
Joined
Aug 29, 2005
Messages
26
Hi,
I have added some code to rename sheets in a spread sheet when it is being opened.

It works great until I get a name which is longer the 31 characters and then I get an error. (due to the limitation on the sheet name to 31 Characters)

so what I need is for the name to be truncated down to 30 characters, not in the original cell but only when the sheet is being named. I am not a VB coder. This code I found in this forum :rolleyes: and used it in my work sheet.

Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
.Activate
.Name = Range("C8")
End With
Next ws
End Sub


Can any one please help. Thanks.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the Board!

How's this:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> Workbook_Open()
    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets
            <SPAN style="color:#00007F">With</SPAN> ws
                .Activate
                <SPAN style="color:#00007F">If</SPAN> .Range("C8") = "" <SPAN style="color:#00007F">Then</SPAN>
                    .Name = .Name
                Else:
                    .Name = Left(.Range("C8"), 30)
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        <SPAN style="color:#00007F">Next</SPAN> ws
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

I also took into account if C8 = ""

Hope that helps,

Smitty
 
Upvote 0
That works great thanks!
I really Apprecieate your help. this is the second time I have posted, and somthing that would have taken me ages to figure out was solved in under 10 minutes... I love it!

You probably don't get the recognition that all you helpers deserve. Once again Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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