Patterns

A pattern is a common way of doing things. Here are the patterns on this site.

Pattern Keywords Situation Actions Referenced in
Input validation loop Loop, Input, Input validation

You want to keep asking the user for input until it's valid.

Use input validation with a flag controlled loop. See the Explanation in this code for an example.

Input-processing-output

Data needs to be transformed into other data, without user interaction after input.

Use abstraction to define each of the three parts, and the way they communicate. Abstraction means that you:

  • Name each subroutine.
  • Specify parameters and, for functions, return values.
Chunks and patterns

Patterns nest

User input and flags

Finding the end
Interfaces, properties, and events

You want to write a GUI program. One with command buttons, textboxes, and other graphical controls.

  • Design the interface, adding controls to a worksheet. Make it easy for the user to do tasks with the interface.
  • Code works by messing with properties of controls, like Caption, Value, Enabled, and Visible.
  • Code runs in response to events, like Click. Code is in event subs, or called from them.
Loop For-Next, Do-Loop
Loop across array

For next. Loop forwards. Loop backwards. Step – 1.

Loop across array and accumulate
Loop and accumulator For-Next
Main program calls subs Subroutine

You have a relatively complex program. It's too big to think about all at once.

Break the program into subs. The main program coordinates passing data between the subs.

Better thunking
Naming controls
Nested loop For-Next
Output to a file - known amount of data Output, File

Your program needs to output data. You know exactly how many elements, and what type each one is.

Use the File open, process, close pattern to open and close the file. Processing uses the Write or Print statements.

Patterns nest
Output to a Web page
Output to screen - known amount of data Output

Your program needs to output data to the user. You know exactly how many elements you want to display, and what type they are. The tip program is an example.

Use MsgBox.

Patterns nest
Output to worksheet cells - known amount of data Output, Worksheet

Your program needs to output data to the user. You know exactly how many elements you want to display, and what type they are. The tip program is an example.

Choose a few cells on a worksheet. Output data to those cells with the Cells method.

Patterns nest
Reading file data into an array
Showing all error messages Error
Switch