Chapter 1: Writing Your First Macro 21: Queryform
Chapter 1: Writing Your First Macro 21: Queryform
The procedure UserForm_Initialize is executed automatically when the form opens. As long as you are in the
development environment, you can simply press the F5 key.
In this procedure the contents of the two label fields are cleared. Furthermore, the variables startcell, firstline,
and lastline are initialized. The variable startcell denotes the first table cell of the vocabulary list and will be
used in the rest of the program as the starting point for addressing further cells in the list. The variables
firstline and lastline provide the first and last line numbers of the vocabulary range.
The calculation of lastline makes use of CurrentRegion, in order to determine the full range of the table
(including the title). Rows decomposes this region into rows, while Count determines their number. (These
properties will be described fully in the first section of Chapter 5.1.)
The procedure ShowNewWord has the task of reading a word (one not yet learned, if possible) from the table
and displaying it in the first label field. The search algorithm is rather trivial: With the random number
function Rnd, which returns a number between 0 and 1, a row (linenr) and test direction (querymode) are
generated. Then, with the method Offset(row, column) either column C or Edepending on querymodeof
the vocabulary table is examined (see Figure 1-11). If the corresponding cell is empty or if it contains the
value 0, then the word is considered not yet learned, and the loop is terminated.
If after maxTries attempts no unlearned word has been found, then a word that has already been learned is
tested. For the running of the program this makes no differencethe word will be read via Offset and
displayed in the first label field. The content of the second label field, which contains the word from the
previous test, is erased. The following three instructions activate the button Continue, and deactivate the
buttons OK and Ask Again Later. Furthermore, the input focus is transferred to the Continue button, so that
this button can be operated with the Return key.
The user now sees a form with a single word and attempts to guess the translation. Finally, he or she clicks on
the Continue button. In the procedure btnNext_Click the word is displayed in the target language in the second
label field. The Continue button is deactivated, and in exchange OK and AGAIN are activated.
Note The procedure name btnNext_Click has its origin in the name of the object (here btnNext) and the name
of the event (Click). To input the code simply execute a double click for the appropriate control in the
form. This causes the lines Private Sub name and End Sub to be entered automatically in the program
code.
After typing in a response, if the user guessed correctly, he or she clicks OK, and the word will not be asked
again. As a result, in btnOK_Click there is stored in column C or E (depending on querymode) how often the
word has been translated correctly. Furthermore, in column D or F is stored how many times the word has
been asked. Calling ShowNewWord triggers the display of the next word.
Here btnAgain_Click functions like btnOK_Click. The only difference is that column D/F is changed, but not
column C/E.
Both procedures btnEdit_Click and btnEnd_Click terminate the form. For this the instruction Unload Me is
used. In the first case the cell pointer is moved to the last displayed word, so that it can be corrected. In the
second case a form is shown to ask whether the modified vocabulary list should be saved.
Additional Code
In order to get the form started correctly, a button (btnStartTrainer) is inserted into the vocabulary table. In the
event procedure the form is displayed with Show. This automatically causes UserForm_Initialize to be
invoked, and the program proceeds as described above.
Of course, there are countless ways in which this program could be improved: a convenient input dialog for
new vocabulary, an options form for controlling the test mode (for example, testing only in one direction), a
more refined algorithm for choosing the next word to test, extending the table with a column showing
pronunciation.
Install the example files in a directory on your hard drive as described in the Appendix.
Note If the programs correspond in some measure to ideas that you have for using Excel, then you can read
the details in the indicated sections. A cross-reference as to which example files are described where in
the book can be found in the Appendix.
Calendar and Holidays
In many Excel applications the problem arises of dealing correctly with holidays. The file 05\Holidays.xls
shows how the occurrence of holidays is calculated. In addition, there is a small program for producing a
calendar for any given year.
Does your spreadsheet need to convert German marks into euros (or some other European currency)? This
process cannot be completely automated, but in 05\Euro.xls you will find some procedures to help you with
the task.
Excel offers the possibility to fashion forms, display them, and evaluate them with program code. A large
number of such forms can be found in the file 07\Userform.xls. The forms can be invoked with a mouse click.
Creating invoices can be greatly simplified through the use of "intelligent" forms. The template 09\Speedy.xlt
provides a simple example. More refined variations on this theme are presented in the example files
DBCars.xls and Vertret.xls.
The extensive possibilities for creating graphs and charts in Excel are often used for presenting large
quantities of data in graphic format. This process lends itself, of course, to automation. In 10\Chart.xls is
demonstrated how (using simulated test data) daily and monthly records of data can be created.
As a member of a car-sharing club, you do not own your own automobile, but rather you borrow one from
your club when you need it. The example 11\DB_Share.xls shows how an "intelligent" form (namely,
09\Share.xlt) can be extended to create a simple database application. With DB_Share you manage the fleet of
cars, the names and addresses of members, and print out invoices for individual trips.