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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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.
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,993
Members
448,539
Latest member
alex78

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