Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to rename worksheet based on partial text of cell? 2

Status
Not open for further replies.

rlee16

Technical User
Jan 19, 2003
44
0
0
HK
I would like to rename my worksheets depending on the presence of a word in a text-filled cell A1. Basically, my problem is that I do not know the syntax to find one element of many in one cell. Do I need to parse out X from the sentence contained in A1 first?


Sub RenameSheet()

Dim wkst as Worksheet
x = "Annual"
y = "1st Quarter"

For Each wkst in Worksheets
If Range("A1") contains X Then
'what is syntax for contains?
wkst.Name = X
Next wkst
End Sub
 
I'm not quite sure what you're looking for here, but I would suggest using the function:

InStr(1, Range("A1").Text, "text you're looking for")

This will return the position of the specified text within Range("A1"), or 0 if it's not found. You could use this in the following fashion:


Sub RenameSheet()
Dim wkst as Worksheet
x = "Annual"
y = "1st Quarter"

For Each wkst in Worksheets
If InStr(1, Range("A1").Text, x) > 0 Then
wkst.Name = X
Next wkst

End Sub

Hope this helps!


 
Thank you Dace! Exactly what I needed.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top