Consulting

Results 1 to 4 of 4

Thread: Solved: Passing Procedure or Function name as variable

  1. #1
    VBAX Mentor asingh's Avatar
    Joined
    Jul 2005
    Posts
    307
    Location

    Solved: Passing Procedure or Function name as variable

    Hi,

    Can I call a funtion or procedure..if the function or procedure name is stored in a variable..

    [VBA]
    Dim strg_var as string

    strg_var = "Name_of_prc_to_call"

    call strg_var
    [/VBA]

    thanks,
    asingh

  2. #2
    For most Office product you can use "Run".
    [VBA]Option Explicit

    Public Sub ExampleOne()
    Dim strSubName As String
    strSubName = "SomeSpecialSub"
    Run strSubName, "I only work in Excel."
    End Sub

    Public Sub SomeSpecialSub(messageText As String)
    MsgBox messageText, Title:="It worked!"
    End Sub[/VBA]

    Note: This is not part of core VBA dll, but a function that is in the Excel Object model and also in Access, Word, Etc. For the most part the syntax and functionality is the same. But if you copy code (such as the snippet below) from, say, Excel to Access you will be running Excel.Application.Run and Access.Application.Run respectively not VBA.Run. And it is technically possible to run across a product that has implemented VBA but not the Run method in which case you will need to adapt your code. But for the most part you should be fine. If you really want to use the VBA library you can use call by name, but you can only call methods that reside in class modules, not standard modules, which could in turn force you to restructure your code.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3
    VBAX Mentor asingh's Avatar
    Joined
    Jul 2005
    Posts
    307
    Location
    Perfect..this will work for me..thanks a lot.

    regards,

    asingh

  4. #4
    hth
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

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