Arguments to procedures in VBA can be optional, and optionally they can have a default value. For example
We can also test for the presence of a variant argument by using isMissing(). See Getting Started with argument passing for how all this works in VBA
There would be no complaints if you called this function with 0, 1 or 2 arguments, but how to detect what was passed?
Let's assume that arg1 was an object.
would be good enough to test if a had been passed with a valid object address, but if arg1 was, say a boolean, and had the value false, then !a would yield the same result as if arg1 was not present. So we need another way.
However, this would fall apart if 'undefined' was a valid value for arg1.
Luckily we can use the typeof operator instead.
Since we are going to be using this a lot, let's make a convenient function of it
So in future we can conveniently say,
And to minimize conversion, do a vbaHack of
We are going to convert an existing VBA project, though, so it's still going to be a pain to deal with each optional argument, especially if it has an optional parameter. Here's a generalized function we can use to deal with missing arguments and their default value
and here is how we use it it.
Transitioning is covered more comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly.
For more like this, see From VBA to Google Apps Script . Why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available. Much of the code referred to here is the mcpher library. You can find out how to include it in your project here.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > From VBA to Google Apps Script >