ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Calling a sub (https://www.excelbanter.com/excel-programming/433311-calling-sub.html)

Dave

Calling a sub
 
Hi,
XL 2003
I'm trying to call a subroutine using a variable to name part of the
subroutine, but I can't find a syntax that works.
ie:
I have several sorting Subs called SortByA, SortByB, SortByC etc
Using an input box, the user is prompted to enter a letter, which I assign
to the variable: 'answer'
I then want to call one of the Subs, like this:
Call "SortBy" & answer
But the above syntax (and lots of others) were rejected.
Can this be done?
Regards - Dave.

Leith Ross[_794_]

Calling a sub
 

Dave;479036 Wrote:
Hi,
XL 2003
I'm trying to call a subroutine using a variable to name part of the
subroutine, but I can't find a syntax that works.
ie:
I have several sorting Subs called SortByA, SortByB, SortByC etc
Using an input box, the user is prompted to enter a letter, which I
assign
to the variable: 'answer'
I then want to call one of the Subs, like this:
Call "SortBy" & answer
But the above syntax (and lots of others) were rejected.
Can this be done?
Regards - Dave.


Hello Dave,

You can do this using "Application.Run"

Application.Run "SortBy" & Answer


--
Leith Ross

Sincerely,
Leith Ross

'The Code Cage' (http://www.thecodecage.com/)
------------------------------------------------------------------------
Leith Ross's Profile: http://www.thecodecage.com/forumz/member.php?userid=75
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=132122


Rick Rothstein

Calling a sub
 
Before attempting to find an answer to your question, let's make sure you
are asking the right question. The naming system of your subroutines and the
method you are using to identify them for execution leads me to believe each
subroutine is the same (that is, same code structure) except for the column
that will be processed. If that is the case, then you are approaching the
problem incorrectly. If I am right, then what you should do is create a
single subroutine with a String argument... pass the letter the user inputs
as the argument when you call the subroutine and then let the subroutine use
that column within the code. It is hard to give you more of an explanation
than that without seeing the subroutine code you are calling.

--
Rick (MVP - Excel)


"Dave" wrote in message
...
Hi,
XL 2003
I'm trying to call a subroutine using a variable to name part of the
subroutine, but I can't find a syntax that works.
ie:
I have several sorting Subs called SortByA, SortByB, SortByC etc
Using an input box, the user is prompted to enter a letter, which I assign
to the variable: 'answer'
I then want to call one of the Subs, like this:
Call "SortBy" & answer
But the above syntax (and lots of others) were rejected.
Can this be done?
Regards - Dave.



Dave

Calling a sub
 
Hi Leith,

Thanks! I used to use 'Application.Run', but when I discovered 'Call' I just
used that 'cause it was so much easier to type, and I thought it did the same
thing. Not quite, evidently...

Regards - Dave.

Dave

Calling a sub
 
Hi Rick,
You are right about that. I have done just as you say. But when I couldn't
seem to use a variable to call a Sub, I became curious as to whether it was
possible.
Thanks for your input.
Dave.

"Rick Rothstein" wrote:

Before attempting to find an answer to your question, let's make sure you
are asking the right question. The naming system of your subroutines and the
method you are using to identify them for execution leads me to believe each
subroutine is the same (that is, same code structure) except for the column
that will be processed. If that is the case, then you are approaching the
problem incorrectly. If I am right, then what you should do is create a
single subroutine with a String argument... pass the letter the user inputs
as the argument when you call the subroutine and then let the subroutine use
that column within the code. It is hard to give you more of an explanation
than that without seeing the subroutine code you are calling.

--
Rick (MVP - Excel)


"Dave" wrote in message
...
Hi,
XL 2003
I'm trying to call a subroutine using a variable to name part of the
subroutine, but I can't find a syntax that works.
ie:
I have several sorting Subs called SortByA, SortByB, SortByC etc
Using an input box, the user is prompted to enter a letter, which I assign
to the variable: 'answer'
I then want to call one of the Subs, like this:
Call "SortBy" & answer
But the above syntax (and lots of others) were rejected.
Can this be done?
Regards - Dave.





All times are GMT +1. The time now is 03:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com