PDA

View Full Version : [SOLVED:] Dynamic Procedure call



CareerChange
03-31-2011, 06:20 PM
I'm probably overlooking the obvious, but can this be done?

I want to avoid coding conditional logic such as a "Select Case" or "If" statement by dynamically creating a procedure name from a derived string.

Here's a code snippet...




Sub TestStub
Dim sProcedure as String, sQtr as String
sQtr = "1Q" ' this variable would normally be set somewhere else
sProcedure = "FindUnassigned " & sType & "Sales"
Call sProcedure
End Sub

Sub FindUnassigned1QSales
'....
End Sub

Sub FindUnassigned2QSales
'....
End Sub

Paul_Hossler
03-31-2011, 06:42 PM
Application.Run ()



Option Explicit

Sub drv()
Dim sProcedure As String
sProcedure = "ONE"
Application.Run (sProcedure)
sProcedure = "TWO"
Application.Run (sProcedure)
sProcedure = "THREE"
Call Application.Run(sProcedure, 2, 3)
End Sub

Sub ONE()
MsgBox "One Called"
End Sub

Sub TWO()
MsgBox "Two Called"
End Sub

Sub THREE(i As Long, j As Long)
MsgBox "Three Called and the answer is " & (i * j)
End Sub


Paul

CareerChange
03-31-2011, 08:51 PM
Perfect!

Thank you Paul!