Stopping duplicate worksheets from being created

Matt0110

New Member
Joined
Feb 21, 2008
Messages
45
Hi all,

I have the following code to produce a copy of a template worksheet and promting the user to give it a name.

Code:
Private Sub CommandButton1_Click()
ActiveWorkbook.Unprotect
If Me.TextBox1.Value = "" Then
Beep
MsgBox "Please give your location a name"
TextBox1.SetFocus
Else
    Sheets("Template").Visible = True
    Sheets("Template").Copy Before:=Sheets(1)
    ActiveSheet.Name = TextBox1.Value
    Sheets("Template").Visible = False
 
    ActiveWorkbook.Protect
    Unload Location
 
End If
 
 
End Sub

However, as you may have guessed, there's no error control and the user can insert a name that already exisits!

Ideally, i'd like a warning to appear explaining that they've tired to create a duplicate but also promting the user to change the name in the textbox refered to above.

Any points would welcome!

Kind regards,

Matt
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
2,578
Try modifying this
TextBox1.SetFocus
Else
Sheets("Template").Visible = True

to this:
Code:
Retry:
TextBox1.SetFocus
Else
Alreadin=0
    For Each ws In Worksheets
    If  TextBox1.Value = ws.Name then Already=1
    Next ws
    If Alreadin > 0 then 
Msgbox("The selected name already exists, retry") : goto Retry
    Sheets("Template").Visible = True

Bye.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,196
Messages
5,835,925
Members
430,396
Latest member
dzifna

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