Code to rename worksheet based on cell value - text too long!

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I need to rename a worksheet based on a value in a cell. The text string is too long, however (the default string will be "yyyymmdd Rotator Test Data FLMxxxx"). I was thinking to add application.worksheetfunction to the code to get the left most characters (which are the most important ones), but the .left function is not available. How can I trim the text to exclude the "FLMxxxx" text from the sheet name? Thanks in advance...
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can use a VBA code to rename the sheet.

VBA Code:
Sub Rename()

Dim ws As Worksheet

Set ws = ActiveSheet

ws.Name = Left(ws.Range("A1").Value, 31)

End Sub
 
Upvote 0
Solution
Hi,

maybe

VBA Code:
With ActiveSheet
  .Name = Left(Replace(.Range("A1").Value, " FLMxxxx", ""), 31)
End With

You should check for illegal characters as well (":", "/", "\", "?", "*", "[", "]").

Holger
 
Upvote 0
Duh my bad, it was simpler than I imagined...
VBA Code:
shNew.Name = Left(Range("B1"), 30)
 
Upvote 0

Forum statistics

Threads
1,215,779
Messages
6,126,843
Members
449,343
Latest member
DEWS2031

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