Excel tip: you can write your own worksheet functions in VBA to make things easier!

If you've ever wanted a worksheet function to do something that isn't supported by the built-in worksheet functions, consider writing your own function using VBA.

It's very easy to do, but there are two drawbacks to be aware of: (1) the workbook must be macro-enabled; (2) your function could perform slower than an equivalent built-in function.

To find out how to write your own, click read more...



You must write your worksheet function in a Module.  To create a Module, open the VBA editor and use Insert > Module.

Within your module create a new function.  I would recommend using uppercase for your function name to keep it consistent with the built-in Excel formulae.  Once you've written your function you can start using it straight away.  Excel will even auto-suggest your function name.

Here's an example:


And here it is in action in cells B1 and B2 (B1 shows even letters, B2 shows odd letters):


Note that custom worksheet functions cannot change cell formatting or ranges.

No comments:

Post a Comment