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
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

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


Thanks in advance
 

Watch MrExcel Video

Forum statistics

Threads
1,123,043
Messages
5,599,479
Members
414,312
Latest member
mikefire911

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