AddressOf

E

E.Z.

Hello,
Doe's "AddressOf" works with Excel macros?
I keep getting a Compile Error for using it.
"Invalid use of AddressOf operator"

This is the code:

Private Sub TestSub()
i = 0
End Sub

Private Function GetAddr(ByVal Addr As Long) As Long
GetAddr = Addr
End Function

Private Sub Test()
Dim Addr as Long
Addr = GetAddr (AddressOf TestSub)
End Sub

The error is for Test Sub
Tnx
 
T

Tim Zych

I don't have much experience with AddressOf, but from what I can see, if
TestSub is moved to a regular module that should fix it.

From my book "VB and VBA In a Nutshell" by Paul Lomax:

AddressOf Operator
Rules at a Glance:
"The callback function must be stored in a code module; attempting to store
it in a class or form module generates a compile-time error, "Invalid use of
AddressOf operator."

When I tested your code, with TestSub moved to a regular code module, it
worked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top