Use Boolean value in an If statement
Sub Use()
Dim userName As String
Dim userBirthday As Date
Dim nameOk As Boolean
nameOk = True
Do
userName = InputBox("What is your first and last name?", "Name")
If (userName <> "") Then nameOk = ValidateName(userName)
Loop While (nameOk = False) Or (userName <> "")
End Sub
Function ValidateName(userName As String) As Boolean
Dim strLength As Integer
Dim I As Integer
Dim numSpaces As Integer
Dim tempString As String
Dim msb As Integer
userName = Trim(userName)
strLength = Len(userName)
For I = 1 To strLength
If Left(userName, 1) = " " Then
numSpaces = numSpaces + 1
End If
userName = Right(userName, Len(userName) - 1)
Next I
If (numSpaces <> 1) Then
ValidateName = False
msb = MsgBox("Please enter just two names separated by one space", vbCritical, "Error")
Else
ValidateName = True
End If
End Function
Related examples in the same category