Excel, the LBW law, range names, form controls and logical formulae

Nearly two years ago I was running an advanced Excel course and needed to create an example of the use of Excel’s logical functions. Given that most of the people we train work in accountancy, our examples are usually based around financial data but, since it was the middle of the 2005 Ashes tour, the cricket LBW (leg before wicket) law sprang to mind. I’ve extended the example to look at the use of range names in making formulae easier to understand, and also to incorporate the use of a simple interactive form control.

Excel and the cricket lbw law

The logic of the lbw law

In the unlikely event that any reader is not fully conversant with the cricket LBW law, I’ll just set out the basics:

If the bowler bowls the ball so that it hits any part of the batsman’s body (usually the leg, but not necessarily) then the batsmen is out if the ball is going on to hit the stumps as long as: the ball has not touched any part of the bat before hitting the batsman – usually known as an ‘edge’; the ball has not pitched outside the line of the leg stump; and the ball has not hit the batsman outside the line of all the stumps when the batsman is playing a stroke. The BBC has an excellent description on their site:

Ways of getting out: leg before wicket

So the main ‘conditions’ we need to check for are:

Was the ball going to hit stumps? (Hitting)

Did the batsman get an edge? (Edge)

Did the ball pitch outside leg? (Outside leg)

Did the ball hit the batsman in line with the stumps? (Hit in line)

Was the batsman playing a stroke? (Playing stroke)

Here is the Excel spreadsheet with the appropriate conditions:

Excel lbw spreadsheet

 

I have used True and False as the entries so we can use the various logical functions to evaluate the result directly. We will be using:

  • IF() – evaluates a statement and returns one result if true or an alternative if false
  • AND() – allows you to check multiple conditions – only if all are true will the result be TRUE
  • OR() – allows you to check multiple conditions – if one or more is true the result will be TRUE
  • NOT() – reverses the logic of a statement – i.e. turns a TRUE FALSE or a FALSE TRUE

If we try and express the logic of the LBW law in English we get something like:

If Not Hitting or Outside Leg, or Edge then ‘Not Out’

If Not Hit in line and Playing stroke then ‘Not Out’

Turning these two statements into logical functions we get four ways of not being out:

OR(NOT(Hitting),Outside Leg, Edge)

AND(NOT(Hit in Line), Playing Stroke)

Combining these gives us:

OR(NOT(Hitting),Outside Leg, Edge, AND(NOT(Hit in Line),Playing Stroke)

This is an OR() function with four arguments, the first three are relatively straightforward, with the fourth being an AND() function with two arguments – both of which must be TRUE for the batsman to be saved. We can now wrap this up in an IF() function which will return ‘NOT OUT’ if any of our four ORs are TRUE and ‘OUT’ if they are all FALSE:

IF(OR(NOT(Hitting),Outside Leg, Edge, AND(NOT(Hit in Line),Playing Stroke),”NOT OUT”,”OUT”)

Let’s see this in Excel using the cell references:

Excel lbw function

Setting up and applying range names

Not the easiest formula to understand! To make it more comprehensible, let’s set up range names for all the TRUE and FALSE cells. First click on cell C3 and then, in the Excel name box type in Hitting:

Excel lbw name

Remember to press the ‘Return’ key after entering the name. Repeat the procedure for the next four cells:

  • C4 – Edge
  • C5 – OutsideLeg
  • C6 – HitInLine
  • C7 – PlayingStroke

Had we created the names before entering our formula, they would have been used automatically when we clicked on the appropriate cells. However, we can Apply the names retrospectively. If we click on our ‘Verdict’ cell then select Insert, Name, Apply (Excel 2007 – Formulas ribbon, Define Name, Apply Names) we can select all of the relevant names, then click OK to change our formula to use the range names rather than the cell references:

Excel lbw apply names

Our formula now looks like this:

Excel lbw applied names

Check boxes

Finally let’s add some check boxes to make it easier for the umpire to choose the right options in the heat of a tense test match.

Display the forms toolbar by choosing View, Toolbars, Forms (Excel 2007 – Developer ribbon, Insert). Click on the Check Box form control and ‘draw’ it next to the ‘Hitting’ label. Delete the associated text, then right click on the control and choose ‘Format Control’. On the ‘Control’ tab, set the cell link to C3 (where the TRUE/FALSE text for ‘Hitting’ is):

Excel lbw check box

Now click on cell B3 and use the fill handle to copy it down to B7. All the check boxes should be copied down. Right-click on each one and set the cell link to the appropriate cell.

See if your understanding of the LBW law agrees with mine!

Advertisements

2 responses to “Excel, the LBW law, range names, form controls and logical formulae

  1. Excellent example & demonstration…

  2. Thanks jose – glad you found it useful

Leave a Reply to simontkb Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s