Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Rename a worksheet after copy...

Hello. I am using the following code to copy a worksheet named DOS5 into a
workbook that already has a sheet called DOS5. When it gets copied I would
like to rename it to the next sequential sheet number (ie: DOS6). Can
someone help?

bk.Sheets("DOS5").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Cou nt)

Thanks,
Cathy
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Rename a worksheet after copy...

I think this is what you are looking for...

Sub test()
Dim strNewName As String
strNewName = "DOS" & CInt(Mid("DOS5", 4, 3)) + 1

If SheetExists(strNewName, ThisWorkbook) Then
MsgBox strNewName & " already exists."
Else
Sheets("DOS5").Copy
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Cou nt)
ActiveSheet.Name = strNewName
End If

End Sub

Public Function SheetExists(SName As String, _
Optional ByVal Wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If Wb Is Nothing Then Set Wb = ThisWorkbook
SheetExists = CBool(Len(Wb.Sheets(SName).Name))
End Function

--
HTH...

Jim Thomlinson


"Cathy W" wrote:

Hello. I am using the following code to copy a worksheet named DOS5 into a
workbook that already has a sheet called DOS5. When it gets copied I would
like to rename it to the next sequential sheet number (ie: DOS6). Can
someone help?

bk.Sheets("DOS5").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Cou nt)

Thanks,
Cathy

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Rename a worksheet after copy...

Hi Cathy are the first three characters always "DOS"

Is you copy DOS7 you want to name it DOS8
Or do you always copy DOS5 ?



--
Regards Ron de Bruin
http://www.rondebruin.nl


"Cathy W" wrote in message ...
Hello. I am using the following code to copy a worksheet named DOS5 into a
workbook that already has a sheet called DOS5. When it gets copied I would
like to rename it to the next sequential sheet number (ie: DOS6). Can
someone help?

bk.Sheets("DOS5").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Cou nt)

Thanks,
Cathy



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Rename a worksheet after copy...

Dim num As Long
Dim fOK As Boolean
Dim sh As Worksheet

num = 5
fOK = False
Do
Set sh = Nothing
On Error GoTo 0
Set sh = ThisWorkbook.Sheets("DOS" & num)
On Error GoTo 0
fOK = Not sh Is Nothing
If fOK Then
bk.Sheets("DOS" & num).Copy
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Cou nt)
Else
num = num + 1
End If
Loop Until fOK

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Cathy W" wrote in message
...
Hello. I am using the following code to copy a worksheet named DOS5 into

a
workbook that already has a sheet called DOS5. When it gets copied I

would
like to rename it to the next sequential sheet number (ie: DOS6). Can
someone help?

bk.Sheets("DOS5").Copy

After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Cou nt)

Thanks,
Cathy



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
Macro: Copy a worksheet and rename it to a specified value mitch Excel Discussion (Misc queries) 2 February 16th 09 05:43 PM
Copy worksheet and rename sheet Ernst - EXE Graphics Excel Discussion (Misc queries) 4 July 25th 08 01:18 PM
Worksheet copy with rename Doug Broad[_4_] Excel Programming 6 March 3rd 06 02:57 PM
Copy/Paste Worksheet to last and rename with input box for value. Dave Peterson[_3_] Excel Programming 2 September 7th 04 08:59 PM
Copy worksheet from multiple files in one DIR to another DIR & rename Mike Taylor Excel Programming 1 July 13th 03 03:28 PM


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