562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Writing functions for Excel using VBA Part three of a three-part series of blogs |
---|
On-line training in writing functions using VBA, for use either within other VBA programs or within Excel itself.
This blog is part of our Excel VBA tutorial. Wise Owl's main business is running classroom-based training courses; have a look at our Excel courses or VBA courses, for example.
|
Suppose you want to work out how tall you are in metres, given your height in feet and inches (these are weird, archaic units of measurement still commonly used in the UK - you don't need to understand what they are to use this example).
Our Excel spreadsheet should look like this:
Type in the formula:
The Metres function should take in two arguments:
and return the number of metres. Here's some code which would do this:
Function Metres(Feet As Single, Inches As Single) As Single
Const MetresPerInch As Single = 0.0254
Const InchesPerFoot As Integer = 12
'first work out how may inches tall the person is
Dim TotalInches As Single
TotalInches = Feet * InchesPerFoot + Inches
'now return number of metres
Metres = MetresPerInch * TotalInches
End Function
You can type in this function within Excel, but it's more fun to use the function wizard:
Click on the cell where the answer should go, then click on the function wizard tool.
You can now choose your function:
Here's the Insert Function dialog box with the steps shown numbered:
The Insert Function dialog box
You now - at last - have help on the arguments, and can complete the dialog box in the usual way:
Complete the dialog box by specifying which cells will provide the feet and inches.
Advanced hint: user-defined functions are "volatile". What this means is if you calculate a spreadsheet containing user-defined functions their value will always be recalculated, even if it can't possibly have changed. This means that if you use VBA functions extensively your spreadsheet calculation speed may drop.
Personally I think Excel has got all of the functions I need, and I only ever use VBA functions within code to make programming easier.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.