Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default Can I use a variable contain the name of a sub or function to call it?

I would like to be able to decide at run time which of several subs to
call and, when writing the code, it may be impossible to even know the name
of the sub.

That is, I cannot use code like:
if i 5 then
call This()
else
call That()
endif

because I have no idea what the names of "This" or "That" may be.

I would like to do something equivalent to:
Sub MyCode(SubName as WhateverIsNeeded)
Call SubName
End Sub

Thanks for any help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,522
Default Can I use a variable contain the name of a sub or function tocall it?

A bit more clarification??


On Nov 23, 8:35*am, wrote:
I would like to be able to *decide at run time which of several subs to
call and, when writing the code, it may be impossible to even know the name
of the sub.

That is, I cannot use code like:
* * * * if i 5 then
* * * * * * * * call This()
* * * * else
* * * * * * * * call That()
* * * * endif

because I have no idea what the names of "This" or "That" may be.

I would like to do something equivalent to:
* * * * Sub MyCode(SubName as WhateverIsNeeded)
* * * * * * * * Call SubName
* * * * End Sub

Thanks for any help.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Can I use a variable contain the name of a sub or function to call it?

Look at the 'CallByName' function.

Is there some reason why you can't include the module at design time?

In either scenario you cite, you still need to know the name of the
procedure to call so why does it matter how you implement this? We need
more info to better help you...

--
Garry

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Can I use a variable contain the name of a sub or function to call it?

wrote in message
...
I would like to be able to decide at run time
which of several subs to call and, when writing
the code, it may be impossible to even know the
name of the sub.


Take a look at the help page for Application.Run. Be sure the limitations
fit your needs.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Can I use a variable contain the name of a sub or function tocall it?

hi,

Sub test()
Dim mc As String, nb As Integer
nb = Application.InputBox(prompt:="Choose a number between 1 and 10", Type:=1)
If nb 5 Then mc = "This" Else mc = "That"
Call MyCode(mc)
End Sub

Sub MyCode(SubName As String)
Application.Run (SubName)
End Sub

Sub This()
MsgBox "This"
End Sub

Sub That()
MsgBox "That"
End Sub


--
isabelle

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call a procedure using a variable ranswrt Excel Programming 3 September 18th 08 01:57 AM
Can a variable be used when declaring an API call ? Mike Iacovou Excel Programming 3 July 23rd 07 03:19 AM
Call procedure using variable donbowyer Excel Programming 2 October 28th 05 09:21 AM
can I call a procedure using a variable Santiago Gomez Excel Programming 8 December 24th 03 09:08 PM
Using variable to call worksheets Calypsoblur Excel Programming 3 November 8th 03 10:47 PM


All times are GMT +1. The time now is 01:36 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"