Check name of new Worksheet before adding it

roulietta2424

New Member
Joined
Aug 16, 2010
Messages
3
Hi

I'm creating a new sheet with my solver and I created a button to assign a name to the worksheet.

If the new name already exists, I get a warning message, the macro fails and a new sheet called eg Sheet 1 is created.

How can I write a "If" to warn the user he entered a name that already exists?

Code :
Private Sub OKButton_Click()

Dim Nameindex As String


With NameSheet

If .Value = "" Then
MsgBox "Enter a valid name for the sheet"
.SetFocus
Exit Sub
End If

ActiveSheet.Name = Nameindex
Range("E3").Value = Nameindex
Range("E3:G3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection.Font
.Name = "Arial"
.FontStyle = "Gras Italique"
.Size = 18
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 30
End With


Unload Me
End With
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi

I'm creating a new sheet with my solver and I created a button to assign a name to the worksheet.

If the new name already exists, I get a warning message, the macro fails and a new sheet called eg Sheet 1 is created.

How can I write a "If" to warn the user he entered a name that already exists?

Code :
Private Sub OKButton_Click()

Dim Nameindex As String


With NameSheet

If .Value = "" Then
MsgBox "Enter a valid name for the sheet"
.SetFocus
Exit Sub
End If

ActiveSheet.Name = Nameindex
Range("E3").Value = Nameindex
Range("E3:G3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
With Selection.Font
.Name = "Arial"
.FontStyle = "Gras Italique"
.Size = 18
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 30
End With


Unload Me
End With
End Sub


Thanks in advance
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,269
Members
449,219
Latest member
daynle

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