Hi all
I have been using an InputBox to collect data, doing a few simple checks using If statements and returning to the beginning of the rountine if my condition is not met.
My question....is it possible to do this without using the Goto rountine
The code example is
Sub Example()
Dim sName As String 'New name for the new worksheet
Dim wks As Worksheet 'Worksheet Object
Start: 'Use InputBox to get the new menu item name
sName = Application.InputBox(Prompt:="Enter the name for the New Menu Item." & vbNewLine & vbNewLine _
& "Try to Keep the Name short" & vbNewLine & vbNewLine & "", Title:="MAIN MENU")
If sName = "False" Then Exit Sub 'This is if the Cancel button is hit
If IsNumeric(Left(sName, 1)) Then 'Check the name doesn't start with a number
MsgBox "The Name Can Not start with a Number", vbInformation
GoTo Start
Else
End If
'Dont allow the name Data, check if name already exists and don't allow blank name
For Each wks In ActiveWorkbook.Worksheets
If sName = wks.Name Then
ElseIf sName = "Data" Then
ElseIf sName = vbNullString Then
MsgBox "Either the Name already exists or it is not a Valid Name", vbInformation
GoTo Start
Else
End If
Next wks
End Sub
I have been using an InputBox to collect data, doing a few simple checks using If statements and returning to the beginning of the rountine if my condition is not met.
My question....is it possible to do this without using the Goto rountine
The code example is
Sub Example()
Dim sName As String 'New name for the new worksheet
Dim wks As Worksheet 'Worksheet Object
Start: 'Use InputBox to get the new menu item name
sName = Application.InputBox(Prompt:="Enter the name for the New Menu Item." & vbNewLine & vbNewLine _
& "Try to Keep the Name short" & vbNewLine & vbNewLine & "", Title:="MAIN MENU")
If sName = "False" Then Exit Sub 'This is if the Cancel button is hit
If IsNumeric(Left(sName, 1)) Then 'Check the name doesn't start with a number
MsgBox "The Name Can Not start with a Number", vbInformation
GoTo Start
Else
End If
'Dont allow the name Data, check if name already exists and don't allow blank name
For Each wks In ActiveWorkbook.Worksheets
If sName = wks.Name Then
ElseIf sName = "Data" Then
ElseIf sName = vbNullString Then
MsgBox "Either the Name already exists or it is not a Valid Name", vbInformation
GoTo Start
Else
End If
Next wks
End Sub