Validation (string, basic)


Normalize, then test with If(s).


You have some string data you don't trust. Could be from a cell, a file, a dialog box, wherevs. You want to check the data.


Some string data.


String data that's valid, or an error message.


An example.

  • tResponse = InputBox("Are you sure you want to detonate (yes/no)?")
  • ' Normalize.
  • tResponse = LCase(Trim(tResponse))
  • If tResponse <> "yes" And tResponse <> "no" Then
  •     MsgBox "Sorry, you must enter yes or no."
  •     End
  • End If

Normalize the input. Then use an If to test for valid responses.

Sometimes you have more than two valid options. You can use one If with the line continuation character:

  • If tMainColor <> "black" _
  •     And tMainColor <> "brown" _
  •     And tMainColor <> "white" _
  •     And tMainColor <> "pink" _
  •     And tMainColor <> "mauve" _
  • Then

Another option is the flag pattern.

Where referenced