PDA

View Full Version : Solved: Passing Procedure or Function name as variable



asingh
10-14-2008, 11:33 PM
Hi,

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


Dim strg_var as string

strg_var = "Name_of_prc_to_call"

call strg_var


thanks,
asingh

Oorang
10-15-2008, 12:00 AM
For most Office product you can use "Run".
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

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.

asingh
10-15-2008, 12:28 AM
Perfect..this will work for me..thanks a lot.

regards,

asingh

Oorang
10-15-2008, 12:35 AM
hth:)