View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
GS[_5_] GS[_5_] is offline
external usenet poster
 
Posts: 226
Default Calling Procedure from Cell value

Raj used his keyboard to write :
Hi,

I want to store Procedure names in cells, loop through the cells and
call the Procedures. I tried storing the cell value in a string
variable eg procstring and using "Call procstring", but it gives an
error. Is there a way to do this?

Thanks in advance for the help.

Regards,
Raj


You need to look up (F1) the CallByName() function if you want to
execute procedures by procedure name.

In this case, you'll need a class module (ie: cEntryPoints) that lists
the procedures as public methods of the class. These don't have to have
the actual code you want to run, but they should call the procedure you
want to run.

Example:
Enter "Macro1" in Range("A1") of the active worksheet

In a class module named cEntryPoints:
Public gsParams As String

Sub Macro1()
'Do stuff or redirect to another procedure
MsgBox gsParams
End Sub


In a standard module named mEntryPoints:
Sub ProcessMyMacro(ProcName As String, Optional Params As String)
Dim oEntryPoints As New cEntryPoints
oEntryPoints.gsParams = Params
CallByName oEntryPoints, ProcName, vbMethod
Set oEntryPoints = Nothing
End Sub

In any standard module, userform, or worksheet/ThisWorkbook
'OnAction for a control:
Sub MyMacro1()
ProcessMyMacro Range("A1").Value, "You called me!"
End Sub

'Inside an event procedu
ProcessMyMacro Range("A1").Value, "You called me!"

Open the Macros dialog and run 'MyMacro1'.

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc