Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 343
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 343
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 257
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default 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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,344
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename Many Worksheets at a time. Shahzad Zameer Excel Discussion (Misc queries) 9 January 6th 07 02:15 PM
Rename Protected Worksheets David Excel Programming 1 January 1st 07 01:58 PM
Rename worksheets with VBA systemx[_7_] Excel Programming 1 March 17th 06 04:54 AM
Rename worksheets Senjaya Excel Programming 3 April 19th 05 02:07 PM


All times are GMT +1. The time now is 04:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"