Exercises

Exercise

Optimal order quantity

Your company buys Stuff from wholesalers, and sells it retail. Work out the optimal order quantity. That is, how many Stuffs you should order each time to minimize cost.

Here's how the worksheet starts:

Start

Users type data into four cells. Validate them.

  • Sales (units per year). Must be a number that is one or greater.
  • Ordering cost ($). The cost of placing one order. Must be a number that is not negative.
  • Carrying cost ($/unit/year). Cost of carrying a unit per year. Storage, insurance, like that. Must be a number that is greater than zero.
  • Whether Smurfs are present. After trimming spaces, should be Y or N, though case doesn't matter.

Show appropriate error messages. Here's a sampling:

Errors

All applicable error messages should show, in red, next to the cells they apply to. Use the flag pattern.

Don't show output if there are any errors. As in the screenshot above.

Here are some more errors.

Errors

If all of the data is OK, compute the optimal order quantity, and show it. The formula from Wikipedia is:

Formula

Here's what the variables in the formula are:

Variables in formula

If there are Smurfs, add one to the order quantity. They always steal just one.

Here's some output.

Output

Another example:

More output

Upload your workbook. The usual coding standards apply.

Exercise

Clown clearance

The sleepy town of Fall's End, Montana, has been invaded by evil aliens! They have turned the townsfolk into lava lamps. There are roughly forty aliens, though the exact number is not known.

Poor research led the aliens to disguise themselves as clowns. They thought they would blend in with the locals.

Marcella has discovered that felines are the only creatures unaffected by the aliens' transmogrifiers. Write an Excel worksheet with some VBA to help her figure out how many leopards, lions, and tigers to rent.

Here's what your solution should look like to start with:

Start

Validate the three inputs. They must be numeric, and cannot be less than zero. In addition, leopards cannot be more than six, lions cannot be more than five, and tigers cannot be more than three.

Here are some error messages:

Errors

If there are any input errors, show all of the error messages at once. Hint: use the flag pattern.

Clear all output and error messages each time the program runs.

Compute and output the cost, and the number of clowns eaten. Each leopard $4,000, and will eat two clowns. Each lion costs $8,500, and will eat four clowns. Each tiger costs $12,800, and will eat seven clowns.

Hint: use singles for all numbers. It's just easier that way.

Here is some output:

Output

Marcella has $90,000 in her wallet. If the total cost is more than $90,000, show a message along with the output, like this:

Output

The usual coding standards apply.

Upload your worksheet.

Exercise

What's the cricket score?

Cricket is a popular sport in British Commonwealth countries, like Australia, and India. Each match has two sides. There are four components to a side's score:

  • Boundaries (sixes): 6 points each
  • Boundaries (fours): 4 points each
  • Singles: 1 point each
  • Sundries: 1 point each

You can read more about cricket scoring, if you want to know more.

Write a VBA program to compute a score given the information above. When the worksheet opens:

MT

The user enters four values, clicks Run, and the program computes the score. For example:

Sample output

Here's some more sample output.

More sample output

All input values must be numeric and zero or more. If there are errors, show messages next to each erroneous cell, in red. Show all applicable error messages at once. For example:

Errors

Be sure to clear old error messages and output when the program runs.

Do not start from scratch. Download this worksheet, and add the code you need.

The usual coding standards apply.

Upload your solution.

Exercise

Goat milk

Estimate the amount of goat milk you'll get per day, based on the number of goats of different breeds you have, and whether you love them or not.

The worksheet starts like this:

Start

For each breed, the user enters the number of goats (a number zero or more), and whether they love the goats. They can can enter y, yes, n, or no. Case and extra spaces shouldn't matter.

If they enter invalid data, they see all applicable error messages, like this:

Errors

If the data's OK, compute the estimated milk production for that scenario. For example:

Output

Here production data, in gallons per day per goat:

Breed Loved Unloved
Alpine 2.2 1.7
Nubian 2.8 2.1
Saanen 1.9 1.5

Here's some more output:

More output

Be sure to clear errors and output at the start of each run.

Upload your workbook. The usual standard apply.

Exercise

Goat fuel

Cthulhu sponsors goat races every month. The course varies in length, and whether there are hurdles or not. Before each race, Cthulhu feeds the goats, so they have the energy they'll need. Write a program to work out the number of kilocalories needed for a race.

You can download a start worksheet. It looks like this:

Start

The user types:

  • The number of goats (must be a number more than zero)
  • The course length (must be a number at least 10)
  • Whether there are hurdles (must be y or n, allow upper- or lowercase, and extra spaces)

Validate the data, showing all appropriate error messages. For example:

Error messages

Make sure error messages and output are cleared each time the program runs.

Some sample output:

Output

Kilocalories is goats times length times 100 divided by 1,000. If there are hurdles, multiply kilocalories times two.

If more than 100 kilocalories are needed, Cthulhu wants to bring in Euphonites, his goat nutritionist. Show a message to that effect:

More output

Upload your workbook. The usual coding standards apply.

Exercise

Goat shirts

Goat

Paula Poundstone is selling shirts for goats on her website. The shirts are made of a remarkably soft tri-poly blend that goats adore. Write a VBA program to compute the cost of an order. The worksheet starts like this (you can download) it):

Start

Customers enter the number of small, medium, and large goat shirts they want. The values must be numeric, and greater than zero. If they aren't, show error messages, like this:

Output

Of course, once an error is fixed, its message should go away. Only show the output if all input is valid.

Shipping can be done by Bonnie, or Toni. Bonnie is cheaper, but her attention wanders. Toni is more likely to get the job done quickly.

The user should enter B or T in the shipping field. Case and extra spaces don't matter. If the user enters something else, show an error message, as shown.

If all the data is OK, compute the total cost. Here are some examples.

Output

Output

Large shirts sell for $28 each, medium for $25, and small for $22. Shipping is $2 per shirt for Bonnie, and $3 per shirt for Toni.

Upload your solution. The usual coding standards apply.

Exercise

Your validation program

Write a program about whatever you like, that has:

  • Input from the worksheet.
  • At least one string, and one numeric input.
  • Output to the worksheet.
  • Validation of all input. Numeric and range check (numeric input), and normalization (string input).
  • All error messages shown on the worksheet.
  • Old output and error messages cleared at start.
  • Output depends on input values, tested with one or more Ifs.

See the other exercises on this page if you need inspiration. Silly is OK.

Upload your workbook. The usual coding standards apply.

Exercise

Exploration expeditions

Write a program to estimate the cost of a goat exploration expedition. You can download a starter workbook. It starts like this:

Start

Users enter the number of goats of different sizes. The values must be numeric, and cannot be less than zero. If there are errors, display error messages all at once, and don't output anything. For example:

Validation errors

Don't forget to erase old error messages, and the output, the next time the program runs.

Estimated days should be a number, at least four. Valid values for terrain are shown in the screen shot. Let users type lowercase if they want, and add extra spaces if they want. Here's another sample screen:

More errors

The terrain is OK, since lowercase and extra spaces are allowed.

Here's some output:

Output

For planning purposes, add three extra days in your calculations. Don't change the user's input, though. For example, if the user estimated six days, do your calculations based on nine days, though leave the cell the user typed in (B8) at six.

Large goats need two ration packs per day. Medium goats need one per day, and small goats need half a pack per day.

Ration packs cost $8 each, unless the expedition is going into jungle. Jungle packs cost $10 each.

Goats need one water jug each, unless they're going into the desert. Then they need two each. Water jugs cost $17 each.

Panniers let goats carry supplies. Here's a small goat with a pannier:

Pannier

Panniers can hold 16 ration packs, or four water jugs. Round up the number of panniers, like this:

  • sPanniersCount = Application.RoundUp(sPanniersCount, 0)

Large and medium goats can carry two panniers each. Small goats can carry one.

Panniers cost $42 each.

There are two warnings you should show, if applicable:

  • When there are more panniers than the goats can carry. Show: You have more panniers than you can carry.
  • Estimated days (the user input) is more than 14. Show: Please check with Tough Toni.

If both warnings are needed, they should be together. For example:

More output

Some more output:

Output

Upload your solution here, not to Moodle. The usual programming standards apply.

Exercise

Bookie bookie book club

Cthulhu is helping his goaty friends form bookie bookie book clubs. Each club has a name (like Bonnie's Ear Burns), and can order three different titles. Cthulhu will cover most of the cost.

You can download a start workbook.

Here's what the order form looks like to start.

Start

The goats enter a name for their club. The name is required. If there is no name, report an error. If the name is just spaces (some goats will try to trick your program), that counts as no name. Show an error message, and end the program.

Error

The goats enter the number of each title they want. The values must be numbers. Here's an error message:

Another error

The numbers can't be negative:

Yet another error

If all the data is OK, complete the computations, like this:

OK

Moby Dicks cost $12.95 each. Eats are $14.95. Fights are $11.95.

If the book cost is less than $50, shipping is 20% of the book cost. If the order is between $50 and $100, then shipping is 10%. Shipping for orders $100 or more is free.

Legal fees are 18% of book costs. There's an ongoing copyright infringement case over the bookie bookie book theme song.

Total cost is books plus shipping plus legal.

The goats pay $8. Cthulhu pays the rest.

Upload your solution. The usual standards apply.

Exercise

Kaba-a-a-a-a-ddi

Kaba-a-a-a-a-ddi is a fave game for goats. Two teams... well, it's hard to describe.

Download the start workbook. Write a program that works out who won a game.

The workbook starts like this:

Start

Users fill in the top part, following the rules given:

  • Goats must be a number, at least 5.
  • Raw score must be a number, at least 0.
  • Shirt color must be green, orange, or blue. Users are allowed to use any combination of upper- and lowercase, and spaces at the start and end of their input.

Make sure you:

  • Show all errors at once.
  • Clear all error messages and output each time the program runs.

Error sample:

Errors

The raw score is adjusted based on the number of players, and the color of their opponent's shirts.

  • For each player above 10, the score is adjusted down by one. For example, if a team had 15 players, 5 points would be subtracted from their raw score.
  • For each player below 10, the score is adjusted up by one. For example, if a team had 5 players, 5 points would be added to their raw score.
  • If a team wears blue shirts, their opponent's score is adjusted up by 5.
  • If a team wears orange shirts, their opponent's score is adjusted down by 5.
  • There is no score adjustment for green shirts.

The winner cell should show "Team 1", "Team 2", or "Draw".

Here's some output:

Output

Upload your solution. The usual standards apply.