Flag

Tags
Summary

A variable that summarizes the results of a bunch of code.

Situation

You have a bunch of code. You want to see whether something happened in the code. It could happen in more than one place in the code. Validation is a common example, where user input could be invalid in a number of different ways.

Needs

A variable you can use as a flag.

Provides

A value in the flag, that you can test for.

Action

Example:

  • 'Get user input.
  • tMainColor = Cells(7, 2)
  • 'Normalize.
  • tMainColor = LCase(Trim(tMainColor))
  • 'Initialize flag.
  • tIsMainColorOk = "no"
  • 'Test for valid colors.
  • If tMainColor = "black" Then
  •     tIsMainColorOk = "yes"
  • ElseIf tMainColor = "brown" Then
  •     tIsMainColorOk = "yes"
  • ElseIf tMainColor = "white" Then
  •     tIsMainColorOk = "yes"
  • ElseIf tMainColor = "pink" Then
  •     tIsMainColorOk = "yes"
  • End If
  • 'Is the color valid?
  • If tIsMainColorOk = "no" Then
  •     'Show error message to the user.
  •     tMessage = "Sorry, " & tMainColor & " is not a valid color."
  •     MsgBox tMessage
  •     'Stop the program.
  •     End
  • End If

Any of the Ifs can change the value of the flag.

See the Basic validation: strings lesson for deets, and a flag goat.