ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Rename worksheets (https://www.excelbanter.com/excel-programming/417761-rename-worksheets.html)

Patrick C. Simonds

Rename worksheets
 
Any way to get this to work? I want the worksheets to rename based on the
values or cells F1 and G1 of each worksheet

Sub Rename_Worksheets()
'
' Macro1 Macro
' Macro recorded 12/19/2005 by Cathy Baker
'

'

'This code runs to rename the worksheets

Dim wks As String
Dim Sh As Worksheet

'Application.ScreenUpdating = False

wks = ActiveSheet.Name

Const sStr As String = "F1 " & "G1"

On Error GoTo ErrHandler
For Each Sh In ThisWorkbook.Worksheets
Sh.Name = Sh.Range(sStr).Value
Next Sh

Worksheets(wks).Activate

Exit Sub
ErrHandler:
'MsgBox "Cell" & sStr & "on sheet" & sh.Name & "is not valid sheet name"
Resume Next

'Application.ScreenUpdating = True
End Sub


Patrick C. Simonds

Rename worksheets
 
Well I have a partial solution to my problem in the form of:

=TEXT(F1,"General") & TEXT(G1," General")

Is there any way I can cause a space between the two results when it is
displayed. Currently it looks like 2009Qrt2. I would like 2009 Qrt2.

"Patrick C. Simonds" wrote in message
...
Any way to get this to work? I want the worksheets to rename based on the
values or cells F1 and G1 of each worksheet

Sub Rename_Worksheets()
'
' Macro1 Macro
' Macro recorded 12/19/2005 by Cathy Baker
'

'

'This code runs to rename the worksheets

Dim wks As String
Dim Sh As Worksheet

'Application.ScreenUpdating = False

wks = ActiveSheet.Name

Const sStr As String = "F1 " & "G1"

On Error GoTo ErrHandler
For Each Sh In ThisWorkbook.Worksheets
Sh.Name = Sh.Range(sStr).Value
Next Sh

Worksheets(wks).Activate

Exit Sub
ErrHandler:
'MsgBox "Cell" & sStr & "on sheet" & sh.Name & "is not valid sheet name"
Resume Next

'Application.ScreenUpdating = True
End Sub



Bob Bridges[_2_]

Rename worksheets
 
Sure, you can make it work, Patrick. The basic structure looks ok, but I see
some errors in particular statements...well, on second thought only one
actual error: Your constant sStr comes across here as two cell addresses
with a space between them, which doesn't really mean anything when used with
Range further down (Sh.Range("F1 G1").Value). But I'm not sure how you need
to fix it. You say you need the new worksheet name to be based on "F1 and
G1"; do you mean the two values concatenated? If so, your assignment
statement would have to be something like this:

Sh.Name = Sh.Range("F1").Value & Sh.Range("G1").Value

Or if you need a space between the two values, of course,

Sh.Name = Sh.Range("F1").Value & " " & Sh.Range("G1").Value

There are a few things I think you're doing unnecessarily, but they're not
errors. For example, you save the name of the active-sheet name up front and
then make sure that sheet is reactivated at the end. But since you never did
anything in your loop to activate any of the sheets you worked with -
renaming a worksheet doesn't activate it - you didn't need to save or restore
any active sheet; this logic doesn't change it at all.

(If you HAD needed to restore the active sheet, your way would work but I
think it'd be slightly simpler to do this:

Set wks = ActiveSheet
' Do your loop here
wks.Activate

That saves the actual sheet object rather than its name, you see. But it's
a detail; your way was fine.)

I THINK that if you change the one Sh.Name assignment, your program will
work correctly. If not, ask again. But here's how I'd simplify the code, if
you ca

Sub Rename_Worksheets()
On Error GoTo ErrHandler
For Each Sh In ThisWorkbook.Worksheets
vn = Sh.Range("F1").Value & Sh.Range("G1").Value
Sh.Name = vn
Next Sh
Exit Sub

ErrHandler:
MsgBox "Error while trying to rename sheet '" & Sh.Name & _
"'. Perhaps " & vn & " isn't a valid sheet name."
Resume Next

End Sub

--- "Patrick C. Simonds" wrote:
I want the worksheets to rename based on the
values or cells F1 and G1 of each worksheet

Sub Rename_Worksheets()
Dim wks As String
Dim Sh As Worksheet
Const sStr As String = "F1 " & "G1"

wks = ActiveSheet.Name

On Error GoTo ErrHandler
For Each Sh In ThisWorkbook.Worksheets
Sh.Name = Sh.Range(sStr).Value
Next Sh

Worksheets(wks).Activate
Exit Sub

ErrHandler:
'MsgBox "Cell" & sStr & "on sheet" & sh.Name & "is not valid sheet name"
Resume Next
End Sub


Gary Keramidas

Rename worksheets
 
i'd use something like this

Dim sStr As String
Set wks = Worksheets("Sheet1")

sStr = wks.Range("F1").Value & " " & wks.Range("G1").Value

--


Gary

"Patrick C. Simonds" wrote in message ...
Well I have a partial solution to my problem in the form of:

=TEXT(F1,"General") & TEXT(G1," General")

Is there any way I can cause a space between the two results when it is
displayed. Currently it looks like 2009Qrt2. I would like 2009 Qrt2.

"Patrick C. Simonds" wrote in message
...
Any way to get this to work? I want the worksheets to rename based on the
values or cells F1 and G1 of each worksheet

Sub Rename_Worksheets()
'
' Macro1 Macro
' Macro recorded 12/19/2005 by Cathy Baker
'

'

'This code runs to rename the worksheets

Dim wks As String
Dim Sh As Worksheet

'Application.ScreenUpdating = False

wks = ActiveSheet.Name

Const sStr As String = "F1 " & "G1"

On Error GoTo ErrHandler
For Each Sh In ThisWorkbook.Worksheets
Sh.Name = Sh.Range(sStr).Value
Next Sh

Worksheets(wks).Activate

Exit Sub
ErrHandler:
'MsgBox "Cell" & sStr & "on sheet" & sh.Name & "is not valid sheet name"
Resume Next

'Application.ScreenUpdating = True
End Sub



ShaneDevenshire

Rename worksheets
 
Hi,

You could try

Dim sh As Worksheet
For Each sh In Worksheets
With sh
.Name = .[F1] & " " & .[G1]
End With
Next sh

--
Thanks,
Shane Devenshire


"Patrick C. Simonds" wrote:

Well I have a partial solution to my problem in the form of:

=TEXT(F1,"General") & TEXT(G1," General")

Is there any way I can cause a space between the two results when it is
displayed. Currently it looks like 2009Qrt2. I would like 2009 Qrt2.

"Patrick C. Simonds" wrote in message
...
Any way to get this to work? I want the worksheets to rename based on the
values or cells F1 and G1 of each worksheet

Sub Rename_Worksheets()
'
' Macro1 Macro
' Macro recorded 12/19/2005 by Cathy Baker
'

'

'This code runs to rename the worksheets

Dim wks As String
Dim Sh As Worksheet

'Application.ScreenUpdating = False

wks = ActiveSheet.Name

Const sStr As String = "F1 " & "G1"

On Error GoTo ErrHandler
For Each Sh In ThisWorkbook.Worksheets
Sh.Name = Sh.Range(sStr).Value
Next Sh

Worksheets(wks).Activate

Exit Sub
ErrHandler:
'MsgBox "Cell" & sStr & "on sheet" & sh.Name & "is not valid sheet name"
Resume Next

'Application.ScreenUpdating = True
End Sub





All times are GMT +1. The time now is 08:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com