Add a new Sheets that (perhaps) already exists

Maurizio

Well-known Member
Joined
Oct 15, 2002
Messages
680
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
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
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
 

Ekim

Well-known Member
Joined
Jun 30, 2002
Messages
1,416
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
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
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>
 

Maurizio

Well-known Member
Joined
Oct 15, 2002
Messages
680
Office Version
  1. 2007
Platform
  1. Windows
Thanks, but I would want that the old sheet "Pippo" was replaced from the new.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,957
Messages
5,621,823
Members
415,859
Latest member
Vain

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
Top