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
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
1,972
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,491
Messages
5,596,462
Members
414,069
Latest member
StudExcel

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