Consulting

Results 1 to 6 of 6

Thread: Solved: Call Function by name contined in String Variable and Return Value

  1. #1
    VBAX Newbie
    Joined
    Aug 2011
    Posts
    5
    Location

    Solved: Call Function by name contined in String Variable and Return Value

    I want to select one function out of many (3)functions, by assigning the name of the selected function <Fun2> into the string variable <FunctionName>.

    I then want to call the selected function and return the value from the function to the variable <Result>. How can I do that?

    The code below illustrates what I want to do, but obviously it is not working.

    [VBA]Public Sub Main()
    Dim FunctionName As String, Result As Double
    FunctionName = "Fun2" ' Selected function
    Result = FunctionName ' I know this does not work...
    MsgBox Result
    End Sub

    Public Function Fun1() As Double
    Fun1 = 1
    End Function

    Public Function Fun2() As Double
    Fun2 = 2
    End Function

    Public Function Fun3() As Double
    Fun3 = 3
    End Function[/VBA]
    Last edited by Bob Phillips; 08-24-2011 at 07:46 AM. Reason: Added VBA tags

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,954
    Location
    Welcome to the forum!

    You may have noticed that xld input code tags for you. If you click the Go Advanced button, there is a Code # button that inserts the tag for you to paste between them.

    cheers

    [vba]Public Sub Main()
    Dim FunctionName As String, Result As Double
    FunctionName = "Fun2" ' Selected function
    Result = Application.Run("'" & ThisWorkbook.FullName & "'!Module1." & FunctionName)
    MsgBox Result
    End Sub[/vba]

  3. #3
    VBAX Newbie
    Joined
    Aug 2011
    Posts
    5
    Location
    Thanks Kenneth! It works....!

    - Lagos

  4. #4
    VBAX Newbie
    Joined
    Aug 2011
    Posts
    5
    Location
    Hi Kenneth,

    I now need to pass an argument <Arg>, in the Function call. Can you please help me?

    Thx,

    Lagos

    [vba]
    Public Sub Main()
    Dim FunctionName As String, Result As Double, Arg as Double
    FunctionName = "Fun2" ' Selected function
    Result = Application.Run("'" & ThisWorkbook.FullName & "'!Module1." & FunctionName) ' How to add <Arg> to this line?
    End Sub [/vba]

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,954
    Location
    Tip: Press F1 when your cursor is in or next to a keyword like Run to see the help in the VBE. From help:
    Syntax

    expression.Run(Macro, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9, Arg10, Arg11, Arg12, Arg13, Arg14, Arg15, Arg16, Arg17, Arg18, Arg19, Arg20, Arg21, Arg22, Arg23, Arg24, Arg25, Arg26, Arg27, Arg28, Arg29, Arg30)

  6. #6
    VBAX Newbie
    Joined
    Aug 2011
    Posts
    5
    Location
    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •