If Then Else

Jabberwokki

New Member
Joined
Dec 2, 2020
Messages
33
Office Version
  1. 2010
Platform
  1. Windows
Hi there and apologies cos i'm sure this has been answered many times before!

I have some code that takes a name from a list drop down cell and creates a sheet with that name. The code then proceeds to copy various info onto that new sheet. However, if the sheet name already exists excel throws its normal error message (obviously). I'm looking to add some code to the following to do a pre check for the name, return a message box with "the name already exists, please select a different one" and exit OR continue if the name doesn't exist.

Any assistance greatly appreciated.

D

VBA Code:
Sub CreateSheet()


    Worksheets("Template").Visible = True
    Worksheets("Front").Activate
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = Range("N2").Value
   
    Sheets("Template").Select
    Cells.Select
    Selection.Copy
   
    Sheets("Front").Select
    Sheets(Range("N2").Value).Activate
   
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
       
    Sheets("Template").Select
    Cells.Select
    Selection.Copy
   
    Sheets("Front").Select
    Sheets(Range("N2").Value).Activate
   
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
       
    Range("F1:H1,V1").Select
    Range("V1").Activate
    Application.CutCopyMode = False
    Selection.ClearContents
   
    Sheets("Template").Select
    ActiveSheet.Shapes.Range(Array("Group 45")).Select
    Selection.Copy
    Sheets("Front").Select
    Sheets(Range("N2").Value).Activate
    Range("A1").Select
    ActiveSheet.Paste
    Selection.ShapeRange.IncrementLeft 12
    Selection.ShapeRange.IncrementTop 16.5
    Range("A5").Select
    ActiveWindow.FreezePanes = True
    Range("M6").Select
    ActiveSheet.Protect
   
    Worksheets("Template").Visible = False
    Worksheets("Front").Activate
   
End Sub
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You could probably use
on error to ask for a suitable name.

This might work better as it will default if the tab already exists but will also default if the string contains something unusable for the file name as well.
 
Upvote 0
You could probably use
on error to ask for a suitable name.

This might work better as it will default if the tab already exists but will also default if the string contains something unusable for the file name as well.
Thanks for the advice cogs. Because the name is selected from a drop down box, i need the message box to let a user know they need to select a different name....
 
Upvote 0
How about
VBA Code:
    Dim ShtName As String

    Worksheets("Template").Visible = True
    Worksheets("Front").Activate
    ShtName = Range("N2").Value
    If ShtName = "" Then Exit Sub
    If Evaluate("isref('" & ShtName & "'!a1)") Then
       MsgBox "Sheet " & ShtName & " already exists"
       Exit Sub
    End If
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = ShtName
 
Upvote 0
Solution
How about
VBA Code:
    Dim ShtName As String

    Worksheets("Template").Visible = True
    Worksheets("Front").Activate
    ShtName = Range("N2").Value
    If ShtName = "" Then Exit Sub
    If Evaluate("isref('" & ShtName & "'!a1)") Then
       MsgBox "Sheet " & ShtName & " already exists"
       Exit Sub
    End If
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = ShtName
Hi Fluff...That's perfect and works a treat. Thank you
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

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