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
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