Add a new Sheets that (perhaps) already exists

Maurizio

Well-known Member
Joined
Oct 15, 2002
Messages
687
Office Version
  1. 2007
Platform
  1. Windows
How can I add a new Sheet named for example "PIPPO" (that perhaps already exists) avoiding any prompt with "error run-time 1004"?

sub addPippo
Sheets.Add
Sheets(ActiveSheet.Name).Name = "PIPPO"
end sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Maurizio,

As far as I am aware, you can't have two (or more) sheets with the same name. (Just imagine the confusion when trying to refer to the sheets!).

What I suggest that you do as part of your 'add a new sheet and name it' routine, is to check whether a particular sheet name exists and, if it does, and a number to the end then loop to check again. If that one also exists increment the number by one and check again and so on.

HTH
 
Upvote 0
Sub addPippo()
On Error GoTo ErrTrap

Sheets.Add
Sheets(ActiveSheet.Name).Name = "PIPPO"

ErrTrap:
Sheets("PIPPO").Activate
MsgBox "Duplicate name ", 16, "Warning " & ActiveSheet.Name
Err.Clear

End Sub

HTH

Mike
 
Upvote 0
On 2002-11-16 14:26, Ekim wrote:
Sub addPippo()
On Error GoTo ErrTrap

Sheets.Add
Sheets(ActiveSheet.Name).Name = "PIPPO"

ErrTrap:
Sheets("PIPPO").Activate
MsgBox "Duplicate name ", 16, "Warning " & ActiveSheet.Name
Err.Clear

End Sub

HTH

Mike

Mike, just a little glitch on the above.
You will need to put in an Exit sub and also a routine to delete the sheet when there
is a duplicate.

May I also suggest another option/way;

<pre/>
Sub Add_Sheet()
On Error Resume Next
With ActiveWorkbook
.Sheets.Add.Name() = "PIPPO"
If Err Then Application.DisplayAlerts = False: ActiveSheet.Delete
Application.DisplayAlerts = True
End With
On Error GoTo 0
End Sub
</pre>
 
Upvote 0
Thanks, but I would want that the old sheet "Pippo" was replaced from the new.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top