Simple clear advice in plain English

Simple programming with Excel

How a quick DIY function can save significant amounts of time

Editing-an-excel-function

The big challenge with getting people excited, or even interested, in programming is coming up with an example that is both simple and useful. That pretty much rules out creating a program from scratch, but there are some useful things you can do with Microsoft Office. Office has its own programming language called Visual Basic for Applications or VBA. I once used this to create my own function in Excel. It took about half an hour and saved my wife hours of work.

The spreadsheet tracked the grades of children in my wife's class. Children were expected to move up one grade every term in each of the four core subjects. Automating this if the grades were numbers would have been laughably simple, =A1+1 would have done the trick.

Sadly it wasn't that simple because the grades were a combination of letters and numbers: 1c, 1b and 1a. It would have been possible to write a formula using the IF command but with a total of 13 possible grades it would have been too long.

The solution was to write a simple funtion that used a different way of working through different options called Switch. Here's how the function looks and then I'll explain how I got there.

Function RaiseOneGrade(Grade As String) 1

RaiseOneGrade = Switch( _
Grade = "p5", "p6", Grade = "p6", "p7", Grade = "p7", "p8", Grade = "p8", "1c", _
Grade = "1c", "1b", Grade = "1b", "1a", Grade = "1a", "2c", Grade = "2c", "2b", _
Grade = "2b", "2a", Grade = "2a", "3c", Grade = "3c", "3b", Grade = "3b", "3a", _
Grade = "3a", "4")
2

End Function 3

It is used in the spreadsheet like this. =RaiseOneGrade(A1)

Line 1 creates the function, gives it the name RaiseOneGrade and then defines what information it will expect. In this case it is a single bit of text that I decied to call Grade.

Section 2 is where the action happens. The Switch command is followed by a series of possible options and what to do if they are true. So it first checks to see if Grade contains p5. If it does it makes the contents of the cell p6. The Function works through the conditions in the order that they are written down.

The underscore characters tell VBA that the command continues on the next line. I added them to make the code easier to read.

Line 3 marks the end of the function.

The Visual Basic Editor can be found in the Tools menu. Click on the Tools menu, then Macro and then Visual Basic Editor. It is important to create the function in the right place. Click on the entry VBAProject (spreadsheet.xls) where spreadsheet.xls is the name of your spreadsheet. Click on the Insert menu and then on Module. Enter the code for the function and click on the save button. Return to Excel to test the function. There is a button with an Excel icon in the toolbar for this.

I hope that gives a taster of some of the useful things you can do with even only a little coding knowledge. Let us know if you have any experiences, advice or questions using the comments box below.

Simple Programming in Excel

Really interesting and informative. Lets have more little bits of code so that we learn as we use it

Posted by Alan Mews, 19 Feb 2011

display:none  

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Recent blog posts

Feeling like a beginner again
While researching a news story I started up my copy of the Windows 8 Consumer Preview in a virtual computer....

Top 5 unexpectedly dangerous animals on Wikipedia
 We know to avoid snakes, spiders and sharks but they're not the only members of the animal kingdom...

Adventures in 3D printing with the Sculpteo iPad app
How much would you pay for a miniature coffee cup with a profile picture of your face on it? What if...

Making the move from inkjet to laser
After nearly 11 years of faithful service, my HP 930cm inkjet printer finally couldn't take it any more...

Latest issue & subscription deals

Most popular articles

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Router

A device used to connect more than one computer or other device to the internet.

Great shopping deals from Computeractive