> I have a macro that I use to make a copy of the active worksheet . It
> works great, if I run the macro again with the same value in "f4", I
[quoted text clipped - 19 lines]
>
> John
That does check for duplicates but it dosen't stop the sheet from
being copied, should I move "ActiveSheet.Copy After:=Sheets(1)" and if
so where should I put it?
John
>How about just checking to see if the rename was successful?
>
[quoted text clipped - 34 lines]
>>
>> John
Dave Peterson - 29 Jan 2007 00:43 GMT
If you don't want the copied sheet to exist if there was an error renaming it,
you could just delete it in that error checking portion.
Dim my_sheet As String
my_sheet = Range("f4").Value
ActiveSheet.Copy After:=Sheets(1)
on error resume next
ActiveSheet.Name = my_sheet
if err.number <> 0 then
application.displayalerts = false
activesheet.delete
application.displayalerts = true
msgbox "rename failed--either invalid name " _
& "or a worksheet with that name already exists"
err.clear
end if
on error goto 0
> That does check for duplicates but it dosen't stop the sheet from
> being copied, should I move "ActiveSheet.Copy After:=Sheets(1)" and if
[quoted text clipped - 40 lines]
> >>
> >> John

Signature
Dave Peterson
John D. Inkster - 29 Jan 2007 01:14 GMT
Got it thanks
John
>If you don't want the copied sheet to exist if there was an error renaming it,
>you could just delete it in that error checking portion.
[quoted text clipped - 59 lines]
>> >>
>> >> John
Bob Phillips - 29 Jan 2007 00:43 GMT
Dim my_sheet As String
Dim sh As Worksheet
my_sheet = Range("f4").Value
On Error Resume Next
Set sh = Worksheets(my_sheet)
On Error GoTo 0
If sh Is Nothing Then
ActiveSheet.Copy After:=Sheets(1)
ActiveSheet.Name = my_sheet
Else
MsgBox "Sheet " & my_sheet & " already exists"
End If

Signature
---
HTH
Bob
(change the xxxx to gmail if mailing direct)
> That does check for duplicates but it dosen't stop the sheet from
> being copied, should I move "ActiveSheet.Copy After:=Sheets(1)" and if
[quoted text clipped - 40 lines]
>>>
>>> John
Hi,
I think that Dave's suggstion was spot on.
I would personally check prior to copying the sheet which involves extra
work but in my opinion does not leave a hole in the process with a worksheet
that has an unkown name.
Try this.
Dim my_sheet As String
Dim ws as worksheet
err.clear
on error resume next
set ws = activesheet
my_sheet = Range("f4").Value
worksheets(my_sheet).activate
if err.number = 0 then
msgbox "Already have a sheet with the name " _
& my_sheet, vbokonly,"Rename error"
else
err.clear
ws.Copy After:=Sheets(1)
ActiveSheet.Name = my_sheet
if err.number <> 0 then
msgbox "rename failed--either invalid name " _
& "or a worksheet with that name already exists"
err.clear
end if
end if
on error goto 0

Signature
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.
> How about just checking to see if the rename was successful?
>
[quoted text clipped - 34 lines]
> >
> > John
John D. Inkster - 29 Jan 2007 00:43 GMT
That works much better.
Thanks Martin
>Hi,
>
[quoted text clipped - 29 lines]
> end if
> on error goto 0
Dave Peterson - 29 Jan 2007 00:50 GMT
Maybe the same idea without activating the sheet--might be useful if that
worksheet exists but is hidden.
Dim my_sheet As String
Dim ws as worksheet
dim testwks as worksheet
set ws = activesheet
my_sheet = Range("f4").Value
set testwks = nothing
on error resume next
set testwks = worksheets(my_sheet)
on error goto 0
if testwks is nothing then
'keep going
else
msgbox "Already have a sheet with the name " _
& my_sheet, vbokonly,"Rename error"
exit sub
end if
ws.Copy _
After:=Sheets(1)
on error resume next
ActiveSheet.Name = my_sheet
if err.number <> 0 then
msgbox "rename failed--invalid name"
application.displayalerts = false
activesheet.delete
application.displayalerts = true
err.clear
end if
end if
on error goto 0
(Untested--watch for typos.)
> Hi,
>
[quoted text clipped - 77 lines]
> >
> > Dave Peterson

Signature
Dave Peterson