30
Teachershare: VBA Ilia Avroutine Royal Grammar School High Wycombe, Bucks

Teachershare : VBA

Embed Size (px)

DESCRIPTION

Teachershare : VBA. Ilia Avroutine Royal Grammar School High Wycombe, Bucks. Bridges the divide between ICT and Computing. Start small – with Excel formulas, combining them Replace them with user functions Create validation rules Play with shapes Add forms and more Visual Basic - PowerPoint PPT Presentation

Citation preview

Page 1: Teachershare : VBA

Teachershare: VBA

Ilia AvroutineRoyal Grammar SchoolHigh Wycombe, Bucks

Page 2: Teachershare : VBA

Bridges the divide between ICT and Computing

• Start small – with Excel formulas, combining them• Replace them with user functions• Create validation rules• Play with shapes• Add forms and more Visual Basic• Great experience designing interfaces and brings

forward the notion of ergonomics• Bring on API calls and OOP

Page 3: Teachershare : VBA

Bridges the divide between ICT and Computing

• Easy syntax, especially if it follows Scratch/Byob and potentially uses block names as variables/function names

• No semicolons at ends of lines, no cryptic brackets/voids• Has incredible wealth of information on the Internet• Can be used to extend the power of Excel for research and

modelling – useful for other subjects and in University• Pupils can program at home or any computer without

needing to install tricky packages (like JAVA)• Is similar to Basic in the Open Office – making the skills

multi-platform transferrable, plus similar to Small Basic

Page 4: Teachershare : VBA

• Subs/Functions/Forms/Visual controls/File Access

• Great to have easy saving in spreadsheets – great for illustrating arrays

• Supports Records – native support for Access files via DAO

• Limited OOP support• Rapid prototyping

Page 5: Teachershare : VBA

• Useful for office employment and automation• Ability to use Windows APIs• Active X controls• The code can write itself – Skynet?• Most skills easily transferrable to other

languages – as our pupils demonstrated, learning ECMA languages on their own

• Any version of Office should work starting from Office 2000 – nothing new to install

• Very visual• Can be extended into ASP, Vbscript

Page 6: Teachershare : VBA

Downsides:

• Slow and verbose• Has no built-in timers (API is a workaround)• Doesn’t indent automatically• More different from pseudocode than Pascal• Is a dead-end language in itself, is quite old-fashioned• No proper OOP inheritance (but there are

workarounds, this is Microsoft, after all)• Runs on top of an office application• Requires relaxing Macro protection – potentially, a

security issue

Page 7: Teachershare : VBA

Transition to other languages

• Pupils are encouraged to have a Word file with accumulation of all the code they’ve written throughout the years

• We aim to use our VLE Wiki/Glossary feature to create a repository of code they understand as it’s at their level

Page 8: Teachershare : VBA

Step 1:

• Logical functions in Excel• Introduce the concept of pseudocode• Copying and pasting Excel functions to other

cells substitutes cells’ addresses – brings a discussion of variables and constants

Page 9: Teachershare : VBA

Step 2: User Defined Functions

• Start easy – a function for converting between metric and imperial

• Then introduce polynomial functions with conversions from degrees C to F

• Then test their pseudocode with MPG to Litres/100 km formula (2/3rds of them usually can’t do it even at this stage)

• Introduce functions with multiple parameters• Array functions – great for anybody who uses Excel anyway• Generation of random numbers and series – Excel gives us

the ability to plot them – nice and visual

Page 10: Teachershare : VBA

Step 3: Data handling Forms

• Introduction to Modules and Forms• Visual Controls• Variable scope• Proper programming information• Might use forms to extend the User Defined

Function examples done previously• Use rich content on forms – Google maps, movie

files, mp3 playlists• Form navigation and interface maps

Page 11: Teachershare : VBA

VBA is not just for programmers

• The industry needs good interface designers, system architects – all of these concepts can be easily taught in VBA without getting bogged down with “proper” programming which might put some of the weaker pupils off

Page 12: Teachershare : VBA

Using Office shapes for modelling

• Railroad crossing• LCD multi-segment digits• Pong• Cash Machine• Battleship• Under development: Pacman, Space Invaders,

Arkanoid, etc.

Page 13: Teachershare : VBA

Forms can also be used for games

• Pupils will face some of the same limitations as the early designers in 1970s – no strong graphics, so have to use very basic objects

Page 14: Teachershare : VBA

Agenda

• Rock, Paper, Scissors game via 2 random number generators– Discussion of pseudocode for the machine-

accessible rules of winning– Use Excel formulas (Lookup, IF)– Show equivalent VBA function

• MPG to Litres per 100 Km conversion– Discussion of pseudocode– Implementation

Page 15: Teachershare : VBA

Temperature Conversion (from http://www.plvwtelco.net)

• Convert Fahrenheit to Celsius• 1.Determine the temperature in Fahrenheit • 2.Using your calculator, subtract 32 • 3.Multiply the result times 5 • 4.Divide that answer by 9 • 5.Your final answer is the temperature in Celsius• For the math purist, the formula is (F-32)*5/9 = C • No calculator handy? Take Fahrenheit, subtract 30, then divide by 2• Convert Celsius to Fahrenheit• 1.Determine the temperature in Celsius • 2.Using your calculator, multiply the temperature times 1.8 • 3.Add 32 to the result • 4.Your final answer is the temperature in Fahrenheit • For the math purist, the formula is (C*1.8)+32=F • No calculator handy? Take Celsius X 2, then add 30

Page 16: Teachershare : VBA

VBA at a glance (assuming Office 2007 but others will work, just different screens) A spreadsheet is a

giant 2D array (3D if you consider using multiple sheets)

VBA gives pupils access to all maths functions of Excel directly

Also – full access to graphics abilities for plotting and tabulation

Page 17: Teachershare : VBA

VBA is hiding behind ALT+F11 or...

Page 18: Teachershare : VBA

Now, your Excel has Developer menu that you can use to enable Macros

Page 19: Teachershare : VBA

Make sure that Macros are enabledMacro viruses are no longer a threat with new version of MS Office, so you should be ok. Nothing replaces a proper antivirus program

Page 20: Teachershare : VBA

VBA Editor window

Page 21: Teachershare : VBA

Familiar VB syntax – so, can leverage all the VB literature

The new bits – VBA objects, right-click this area to add Code modules and Forms

Page 22: Teachershare : VBA

VBA objects:

• Workbooks• Sheets• Shapes• Forms• Ranges (cells)• Any ActiveX object – from Quicktime video to

Flash movie• Use Macros to explore VBA objects

Page 23: Teachershare : VBA

Here is a simple example – a user defined function converting centimetres to inches

Page 24: Teachershare : VBA

Give it a meaningful name. You can have as many as you like

Page 25: Teachershare : VBA

The function code itself... Functions take parameters and are typed

Function cm2inch(ByVal inputCM As Single) As Singlecm2inch = inputCM / 2.54End Function

Page 26: Teachershare : VBA

The moment you are finished coding the function, you can switch back to the spreadsheet view and Excel will

recognize your function and add it to its built-in list

Page 27: Teachershare : VBA

Behaves just like native functions and takes a cell that you click on as a parameter

Function cm2inch(ByVal inputCM As Single) As Singlecm2inch = inputCM / 2.54End Function

Page 28: Teachershare : VBA

The result, with some extra formatting

Page 29: Teachershare : VBA

Something a bit more involved. VBA being partially OOP, we have arrays(1st function) and also

collections(2nd function below)Function sumOfCubes(ParamArray otherElements() As Variant)Dim counter As Integer For counter = 0 To UBound(otherElements) sumOfCubes = sumOfCubes + otherElements(counter) ^ 3 Next counterEnd Function

Function sumOfCubes2(ByVal otherElements As Range)Dim element As Range For Each element In otherElements.Cells sumOfCubes2 = sumOfCubes2 + element ^ 3 NextEnd Function