UDFs are a way to simplify your Excel worksheets. They also enable you to use more complicated formulae than you can normally manage to type into a single cell. They are also a route into programming in Visual Basic for Applications, VBA.
For this project you are going to write a UDF which converts a decimal grade, such as 5.1, into a number and letter grade, like 5b.
Step 1. Find the VBA EditorOpen a new workbook. Click on Tools, then select Macro and then select Visual Basic Editor.
Step 2 Create a Module.Now right click in the Project-VBA Project window and select Insert and Module.
If the Project-VBA Project window isn't visible then follow step 2a → Step 3 Write a FunctionYou should now have a cursor blinking at you in the window which shows the code for module1.
|
Step 1a. Enable Macros.If these menus are grayed out then try selecting Tools, then options.
Then click on the security tab followed by the Macro Security button.
Finally change the security level button to medium. Click okay and return to the worksheet screen. If this doesn't work then your network settings etc might need changed. Seek advice from a computer service droid.
NB. If you now download dodgy Excel spreadsheets from dodgy sources and allow their dodgy macros to run then you can COMPLETELY MESS UP YOUR COMPUTER! so only run content from people that you trust or, better still, write your own dodgy macros.Step 2a. Find the Project Explorer.If the Project Explorer window isn't visible then click on View and Project Explorer.
|
Step 4Now type the code for the function into the window. The machine will add End Function for you automatically. Excel has comprehensive help files to explain what all the different commands do. The main problem is knowing enough American computer geek speak to guess what they call 'highest common factor' etc. (GCD, work it out.) You can either type it or cut and paste from below the picture. You don't need the comment lines which begin with a '. Excel colours things in to differentiate commands, variables and comments.
| |
|
Function declett(no1) 'converts a decimal result grade such as 6.5 into a standard grade such as 6b. int1 = Int(no1) int2 = Int(no1 * 10) frontbit = int1 backbit = int2 - 10 * int1 If backbit < 4 Then endbit = "c" End If If (backbit >= 4 And backbit < 7) Then endbit = "b" End If If backbit >= 7 Then endbit = "a" End If declett = frontbit & endbit End Function |
Step 5 Test ItTo call your function simply enter it into a cell just like a standard Excel function.
Lots of things can go wrong at this point: If nothing happens when you press Return check that you have got the function typed in absolutely right. The formula will only work after you have got the function right. It won't go back to cells which had the formula entered prior to you getting it right. So once you have it working apply it to all the cells that need it. If it has worked then you should get something like this.
|
Going FurtherThere are limits on the acrobatics that UDFs can perform. If you wish to do even more complex things to your spreadsheet then you need to investigate macros. These enable you to do lots of fun things. | |