Call VBA class module in Excel formula

I know that a module function can be called in a formula like this:

=GetMyModuleFunction()

Now I would like to call the class module function. I don’t know if a module class can be instantiated in the formula, so I created a function in the module, so I can call it the same way =GetMyModuleFunction():

' Class Module MyClassModule:
Public Property Get MyProperty() As String
    MyProperty = "Hello World!"
End Property

Public Function GetMyFunction() As String

    GetMyFunction = "Hello World!"

End Function
' End Class Module MyClassModule

' Module MyModule:
Public Function GetMyClassModule() As MyClassModule

   Set GetMyClassModule = New MyClassModule

End Function
' End Module MyModule

So after that I tried in the line of formulas:

=GetMyClassModule().GetMyFunction()
=GetMyClassModule().MyProperty

What the error dialog shows is that the formula is invalid. Is it not possible, what am I trying to achieve here? I am currently using modules instead, but functions and subtitles with duplicate names are confused and prone to usage errors in modules.

+4
source share
1 answer

: VBA,

, Excel . Excel .

(UDF) :

Public Function GetMyClassModuleFunction() As String

    GetMyClassModuleFunction = GetMyClassModule.GetMyFunction()

End Function
+3

Source: https://habr.com/ru/post/1624519/


All Articles