Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / January 2007

Tip: Looking for answers? Try searching our database.

worksheet with same name error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John D. Inkster - 28 Jan 2007 21:50 GMT
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
get a vb error. Is there a way to check the name in "f4" against all
worksheet names (there could be 50 sheets) and trap the error?

Dim my_sheet As String
   my_sheet = Range("f4").Value
   ActiveSheet.Copy After:=Sheets(1)
   ActiveSheet.Name = my_sheet

I will then use:

MsgBox "Sheet Saved", vbOKOnly + vbExclamation
Else
MsgBox "Duplicate name  Sheet Not Saved", _
vbOKOnly + vbExclamation

End If
End Sub

Thanks for any help!

John
Dave Peterson - 28 Jan 2007 22:28 GMT
How about just checking to see if the rename was successful?

   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
       msgbox "rename failed--either invalid name " _
               & "or a worksheet with that name already exists"
       err.clear
   end if
   on error goto 0

> 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

Signature

Dave Peterson

John D. Inkster - 29 Jan 2007 00:06 GMT
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
Martin Fishlock - 29 Jan 2007 00:10 GMT
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

John D. Inkster - 29 Jan 2007 01:17 GMT
Thanks Guys, I'm glad you had nothing better to do on a Sunday
Either.............. <G>

John Inkster

>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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.