Word 2000: Call a subroutine dynamically with VBA

V

VBA Coder

I would like to call a subroutine dynamically based on a string that I am
building. I cannot seem to get the syntax to work. I have a listbox with
over 100 items in it, and each item within the listbox calls it's own
subroutine. With a simple string variable, I'd like to read the value
selected from the listbox and call it's subroutine, which is the same name.
Some very simple sample code is below:

Old Code Method:
Select Case Me.listItemList.Text
Case "Sub1"
Sub1_Proc
Case "Sub2"
Sub2_Proc
Case "Sub3"
Sub3_Proc
Case "Sub4"
Sub4_Proc
etc...will continue up past 100 with the same logic
End Select

New Code:
I would like to just reference the listbox and append "_Proc" to the end and
then call that subroutine, something like below:
Dim sProc as string
sProc = Me.listItemList.Text & "_Proc"
Call sProc
 
J

Jonathan West

Hi VBA coder

There are two commands which can do this, you can work with either depending
on what works best for your specific requirement. There is the
Application.Run method, and the CallByName function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top