Weird observation:
Normally when I want to save an address to a function in a variable, I do something like this:
Function getAddress(ByVal func As LongPtr) As LongPtr
getAddress = func
End Function
Sub printAddress()
Dim functionPointer As LongPtr
functionPointer = getAddress(AddressOf myFunc)
Debug.Print functionPointer
End Sub
However I just discovered I can use a 1-liner
functionPointer = VBA.CLngPtr(AddressOf myFunc)
while
functionPointer = CLngPtr(AddressOf myFunc)
... does not work and raises
Compile error:
Expected: expression
What's going on? The only difference as far as I'm aware is that CLngPtr
is declared in the globals (class?) whereas VBA.CLngPtr
is explicitly qualified, but I have no idea why that would lead to the observed behaviour (they both point to the same function don't they?)
What Will Replace VBA? Short answer: JavaScript. JavaScript is a language Microsoft has been heavily investing in as it moves to unify Office across all devices (PC, Mac, Browser, & Mobile).
VBA stands for Visual Basic Analysis. Excel VBA is Microsoft's programming language for Office applications such as MS-Excel, MS-Word, and MS-Access. Macros are what most people who write VBA code use.
Python is easier to learn and master, unlike Excel, which includes a personalized language known as VBA that is complex to master and execute. Transitioning from Excel to Python enables users to enjoy various benefits, such as an open-source coding platform, many volunteer contributors, and free libraries.
VBA is an interpreted language, while VB.NET is compiled. If you are interested in Office programming, you might consider doing your next Office project in VB.NET and VSTO, which is the modern alternative to VBA.
This would be not very obvious if you used the default IDE settings which the keywords and identifiers aren't really set that differently. Here's how it looks when you use different colors:
You can see that CLngPtr
lights up like a christmas tree and looks just like any other keywords. Compare this with Abs
, which is also a function but stays light blue, as if it was just an identifier.
This is a hint that CLngPtr
is optimized by VBA compiler so it's actually inlining the method1, which is why you get error if you try to use CLngPtr
as an expression. However, a VBA.CLngPtr
is a proper function and thus can be used as a part of an expression but with very slight performance penalty due to non-optimizing route.
You would see the same thing with say, CLng
or any conversion functions, and even Mid
the statement (not function). There are several functions within VBA
that may get inlined by the compiler and usually can be differed by whether they turn into keywords or not. Note also that the parenthesis are colored differently.
Heck, even Debug.Print
gets the special treatment, too and if you are familiar with it, you may know that it's not exactly a class nor a module, yet you can't Print
without Debug
.
C***()
and VBA.C***()
are basically the same thing. However, the VBA compiler can and will try to optimize those bits, by internally rearranging the machine instructions for converting (or whatever the inlined function is doing). The effect of rearranging the instruction is that it might no longer be compatible in all contexts. In this case, I can imagine (but do not know for a fact!) that CLngPtr()
's inlined instructions returns a value, rather than a reference, which is incompatible for a parameter declaration, which is why we get a syntax error when we try to use it as a parameter. Note this does not happen with AddressOf
-- any other function on the LHS will have the same syntax error, so it has nothing to do with the AddressOf
and everything with the method being inlined.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With