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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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