Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
TKM TKM is offline
external usenet poster
 
Posts: 33
Default Renaming wrong worksheet. Why?

I noticed this code is naming the wrong worksheet.. It assumes the newest
sheet is the one to be renamed and keeps renaming "sheet3". Any help
appreciated.

Public Function CopySheetAnalyst(xlSheet As Worksheet, Index) As Worksheet
'
' Macro2 Macro
' Macro recorded 7/11/2003 by SILVESC
'
Dim xName As String
Dim Basename As String
Dim x As Long

Call xlSheet.Copy(After:=Sheets(Sheets.Count))
Basename = Worksheets("CoverSheet").Range("B5").Offset(0, Index).Value
xName = Basename
On Error GoTo TryAgain
Sheets(Sheets.Count).Name = xName
On Error GoTo 0
Set CopySheetAnalyst = Sheets(Sheets.Count)
Exit Function
TryAgain:
x = x + 1
xName = Basename & "-" & x
Resume
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Renaming wrong worksheet. Why?

It looks to me like it is renaming the sheet it is told to rename.

On Error GoTo TryAgain
Sheets(Sheets.Count).Name = xName
On Error GoTo 0
'Set CopySheetAnalyst = Sheets(Sheets.Count)
'Exit Function
'TryAgain:
x = x + 1
xName = Basename & "-" & x

It is told that the last sheet is xName then it is told to rename the last
sheet.


"TKM" wrote in message
...
I noticed this code is naming the wrong worksheet.. It assumes the newest
sheet is the one to be renamed and keeps renaming "sheet3". Any help
appreciated.

Public Function CopySheetAnalyst(xlSheet As Worksheet, Index) As Worksheet
'
' Macro2 Macro
' Macro recorded 7/11/2003 by SILVESC
'
Dim xName As String
Dim Basename As String
Dim x As Long

Call xlSheet.Copy(After:=Sheets(Sheets.Count))
Basename = Worksheets("CoverSheet").Range("B5").Offset(0, Index).Value
xName = Basename
On Error GoTo TryAgain
Sheets(Sheets.Count).Name = xName
On Error GoTo 0
Set CopySheetAnalyst = Sheets(Sheets.Count)
Exit Function
TryAgain:
x = x + 1
xName = Basename & "-" & x
Resume
End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default Renaming wrong worksheet. Why?

The issue is in this line:
* * Sheets(Sheets.Count).Name = xName

This causes it to refer to the last sheet in the array of sheets. You
can refer to the first sheet like this:
Sheets(1).Name = xName
Or a named sheet like this:
Sheets("Sheet1").Name = xName
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Renaming wrong worksheet. Why?

I like to just check in line instead of branching:

Option Explicit
Sub testme()
Dim wks As Worksheet
Dim dummy As Long
Set wks = CopySheetAnalyst(Worksheets("Sheet8"), dummy)
MsgBox wks.Name
End Sub
Public Function CopySheetAnalyst(xlSheet As Worksheet, Index As Long) _
As Worksheet

Dim xName As String
Dim Basename As String
Dim x As Long

xlSheet.Copy _
After:=xlSheet.Parent.Sheets(xlSheet.Parent.Sheets .Count)

Basename = "xxx"
'Basename = Worksheets("CoverSheet").Range("B5").Offset(0, Index).Value
xName = Basename
x = 0
Do
If x 0 Then
xName = Basename & "-" & x
End If
On Error Resume Next
xlSheet.Parent.Sheets(xlSheet.Parent.Sheets.Count) .Name = xName
If Err.Number < 0 Then
Err.Clear
x = x + 1
Else
'woohoo, it worked!
Exit Do
End If
Loop
Set CopySheetAnalyst = xlSheet.Parent.Sheets(xName)
End Function

=====
But if you want to use your code, start at Chip Pearson's site:
http://www.cpearson.com/Excel/ErrorHandling.htm

Ps. I changed your code so that the workbook is qualified (xlsheet.parent).





TKM wrote:

I noticed this code is naming the wrong worksheet.. It assumes the newest
sheet is the one to be renamed and keeps renaming "sheet3". Any help
appreciated.

Public Function CopySheetAnalyst(xlSheet As Worksheet, Index) As Worksheet
'
' Macro2 Macro
' Macro recorded 7/11/2003 by SILVESC
'
Dim xName As String
Dim Basename As String
Dim x As Long

Call xlSheet.Copy(After:=Sheets(Sheets.Count))
Basename = Worksheets("CoverSheet").Range("B5").Offset(0, Index).Value
xName = Basename
On Error GoTo TryAgain
Sheets(Sheets.Count).Name = xName
On Error GoTo 0
Set CopySheetAnalyst = Sheets(Sheets.Count)
Exit Function
TryAgain:
x = x + 1
xName = Basename & "-" & x
Resume
End Function


--

Dave Peterson
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
Worksheet renaming jwilliam33 Excel Discussion (Misc queries) 5 December 6th 08 09:14 PM
Worksheet Renaming Eric Excel Discussion (Misc queries) 2 July 26th 07 02:44 PM
Worksheet Renaming Eric Excel Worksheet Functions 1 July 26th 07 02:38 PM
need help renaming a worksheet jonwyn Excel Discussion (Misc queries) 2 March 1st 05 08:34 PM
Renaming the Worksheet through VBA abxy[_24_] Excel Programming 4 February 9th 04 07:32 PM


All times are GMT +1. The time now is 09:19 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"