Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Renaming Worksheets with For Each Loop

Hello

I am trying to loop through the Worksheets and rename the various
worksheets based on the name in Cell A1. The below script will rename
the first sheet "Means" but then I get an error
"Cannot rename a sheet to the same name as another sheet"

What is wrong with my script?



Sub A0Rename()

For Each sh In Worksheets

If ActiveSheet.Cells(1, 1).Value = "Descriptives" Then sh.Name = "Means"
If ActiveSheet.Cells(1, 1).Value = "ANOVA" Then sh.Name = "ANOVA"

If ActiveSheet.Cells(1, 1).Value = "Test of Homogeneity of Variances"_
Then sh.Name = "HOV"

If ActiveSheet.Cells(1, 1).Value = "Multiple Comparisons" Then sh.Name_
= "Pairwise"

Next sh

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Renaming Worksheets with For Each Loop

You're always looking at cell A1 so that means you try to rename every sheet
with whatever is in that cell. The first sheet gets renamed, the second
sheet stops the code.

Regards

Trevor


wrote in message
...
Hello

I am trying to loop through the Worksheets and rename the various
worksheets based on the name in Cell A1. The below script will rename
the first sheet "Means" but then I get an error
"Cannot rename a sheet to the same name as another sheet"

What is wrong with my script?



Sub A0Rename()

For Each sh In Worksheets

If ActiveSheet.Cells(1, 1).Value = "Descriptives" Then sh.Name = "Means"
If ActiveSheet.Cells(1, 1).Value = "ANOVA" Then sh.Name = "ANOVA"

If ActiveSheet.Cells(1, 1).Value = "Test of Homogeneity of Variances"_
Then sh.Name = "HOV"

If ActiveSheet.Cells(1, 1).Value = "Multiple Comparisons" Then sh.Name_
= "Pairwise"

Next sh



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Renaming Worksheets with For Each Loop

You were always looking at the activesheet to get the value of A1 (so the
value never changed) - while looping through the sheet, so on the second
sheet, you tried to rename it the same name as the first sheet.

Sub A0Rename()

For Each sh In Worksheets

If sh.Cells(1, 1).Value = "Descriptives" Then sh.Name = "Means"
If sh.Cells(1, 1).Value = "ANOVA" Then sh.Name = "ANOVA"

If sh.Cells(1, 1).Value = "Test of Homogeneity of Variances"_
Then sh.Name = "HOV"

If sh.Cells(1, 1).Value = "Multiple Comparisons" Then sh.Name_
= "Pairwise"

Next sh

End Sub

--
Regards,
Tom Ogilvy

wrote in message
...
Hello

I am trying to loop through the Worksheets and rename the various
worksheets based on the name in Cell A1. The below script will rename
the first sheet "Means" but then I get an error
"Cannot rename a sheet to the same name as another sheet"

What is wrong with my script?



Sub A0Rename()

For Each sh In Worksheets

If ActiveSheet.Cells(1, 1).Value = "Descriptives" Then sh.Name = "Means"
If ActiveSheet.Cells(1, 1).Value = "ANOVA" Then sh.Name = "ANOVA"

If ActiveSheet.Cells(1, 1).Value = "Test of Homogeneity of Variances"_
Then sh.Name = "HOV"

If ActiveSheet.Cells(1, 1).Value = "Multiple Comparisons" Then sh.Name_
= "Pairwise"

Next sh



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Renaming Worksheets with For Each Loop

If u want to rename a sheet based on the name of the A1 cell of that sheet u
need to write something like this :

Sub A0Rename()

For Each sh In Worksheets
If sh.Cells(1, 1).Value = "Descriptives" Then sh.Name = "Means"
If sh.Cells(1, 1).Value = "ANOVA" Then sh.Name = "ANOVA"

If sh.Cells(1, 1).Value = "Test of Homogeneity of Variances"_
Then sh.Name = "HOV"
If sh.Cells(1, 1).Value = "Multiple Comparisons" Then sh.Name_
= "Pairwise"

Next sh

Truly yours,

René.
a écrit dans le message de
...
Hello

I am trying to loop through the Worksheets and rename the various
worksheets based on the name in Cell A1. The below script will rename
the first sheet "Means" but then I get an error
"Cannot rename a sheet to the same name as another sheet"

What is wrong with my script?



Sub A0Rename()

For Each sh In Worksheets

If ActiveSheet.Cells(1, 1).Value = "Descriptives" Then sh.Name = "Means"
If ActiveSheet.Cells(1, 1).Value = "ANOVA" Then sh.Name = "ANOVA"

If ActiveSheet.Cells(1, 1).Value = "Test of Homogeneity of Variances"_
Then sh.Name = "HOV"

If ActiveSheet.Cells(1, 1).Value = "Multiple Comparisons" Then sh.Name_
= "Pairwise"

Next sh



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
Renaming worksheets Teeny Excel Discussion (Misc queries) 8 August 18th 09 10:21 PM
Renaming worksheets James Excel Worksheet Functions 0 May 12th 09 11:45 PM
RENAMING WORKSHEETS LEOPARDSHIDEAWAY Excel Worksheet Functions 1 July 26th 07 10:27 PM
Renaming worksheets Mike Allen Excel Discussion (Misc queries) 8 January 21st 07 02:15 AM
Renaming Worksheets Steve Walford Excel Worksheet Functions 3 April 1st 05 09:29 PM


All times are GMT +1. The time now is 09:13 PM.

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

About Us

"It's about Microsoft Excel"