PC Review


Reply
Thread Tools Rate Thread

Application.Run with variable number of arguments

 
 
Doug Glancy
Guest
Posts: n/a
 
      6th Jul 2007
Hello,

I'm trying to create a generic sub that will call Application.Run with a
Worksheet.Module.Sub passed to it and also a variable number of arguments.
Is this possible? For example, if wishing made it so, it would run like
this:

Sub app_run(macro_to_run as string, params as variant)
Application.Run macro_to_run, params
End Sub

In my dreams, I'd call it something like this:

Call app_run("MyBook.xls!MyModule.MyMacro", array("test 1", 1)

And lo and behold MyMacro would run:

Sub MyMacro(str1 as String, lng1 as Long)
....


If that's not possible, is there any way to pass one string to my app_run
sub and have it parsed for the Workbook.Module.Macro and the args, e.g.:

Call app_run(("MyBook.xls!MyModule.MyMacro, test1, 1")

Thanks in Advance,

Doug

 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      6th Jul 2007
Doug,

This is how that works:

oXL.Run "MyBook.xls!MyModule.MyMacro", var1, var2

RBS


"Doug Glancy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I'm trying to create a generic sub that will call Application.Run with a
> Worksheet.Module.Sub passed to it and also a variable number of arguments.
> Is this possible? For example, if wishing made it so, it would run like
> this:
>
> Sub app_run(macro_to_run as string, params as variant)
> Application.Run macro_to_run, params
> End Sub
>
> In my dreams, I'd call it something like this:
>
> Call app_run("MyBook.xls!MyModule.MyMacro", array("test 1", 1)
>
> And lo and behold MyMacro would run:
>
> Sub MyMacro(str1 as String, lng1 as Long)
> ...
>
>
> If that's not possible, is there any way to pass one string to my app_run
> sub and have it parsed for the Workbook.Module.Macro and the args, e.g.:
>
> Call app_run(("MyBook.xls!MyModule.MyMacro, test1, 1")
>
> Thanks in Advance,
>
> Doug


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      6th Jul 2007
Doug,

Probably best to use ParamArray.

In the macro to be run, declare a single variant argument, where you know
whether it expects multiple or single parameters

Public Sub Test(p1)
Dim i As Long
For i = LBound(p1) To UBound(p1)
MsgBox p1(i)
Next i
End Sub

and in the caller, use paramarrays to give the flexibility, passing it as a
single variant

Sub app_run(macro_to_run As String, ParamArray params())

Application.Run macro_to_run, params()
End Sub

and call it like so

Public Sub Test_app_run()
app_run "Book2!Test", 1, 2, 3
End Sub




--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Doug Glancy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I'm trying to create a generic sub that will call Application.Run with a
> Worksheet.Module.Sub passed to it and also a variable number of arguments.
> Is this possible? For example, if wishing made it so, it would run like
> this:
>
> Sub app_run(macro_to_run as string, params as variant)
> Application.Run macro_to_run, params
> End Sub
>
> In my dreams, I'd call it something like this:
>
> Call app_run("MyBook.xls!MyModule.MyMacro", array("test 1", 1)
>
> And lo and behold MyMacro would run:
>
> Sub MyMacro(str1 as String, lng1 as Long)
> ...
>
>
> If that's not possible, is there any way to pass one string to my app_run
> sub and have it parsed for the Workbook.Module.Macro and the args, e.g.:
>
> Call app_run(("MyBook.xls!MyModule.MyMacro, test1, 1")
>
> Thanks in Advance,
>
> Doug



 
Reply With Quote
 
Doug Glancy
Guest
Posts: n/a
 
      6th Jul 2007
Bob,

Good to hear from you.

That is a workable idea, although since the subs that will be called are
multiple and existing, I'm not sure it's practical since it would involve
rewriting them. I have a workaround, I think, but was especially interested
to see if Application.Run could call these existing subs, with an unknown
number of params.

Thanks,

Doug

"Bob Phillips" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Doug,
>
> Probably best to use ParamArray.
>
> In the macro to be run, declare a single variant argument, where you know
> whether it expects multiple or single parameters
>
> Public Sub Test(p1)
> Dim i As Long
> For i = LBound(p1) To UBound(p1)
> MsgBox p1(i)
> Next i
> End Sub
>
> and in the caller, use paramarrays to give the flexibility, passing it as
> a single variant
>
> Sub app_run(macro_to_run As String, ParamArray params())
>
> Application.Run macro_to_run, params()
> End Sub
>
> and call it like so
>
> Public Sub Test_app_run()
> app_run "Book2!Test", 1, 2, 3
> End Sub
>
>
>
>
> --
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my
> addy)
>
> "Doug Glancy" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello,
>>
>> I'm trying to create a generic sub that will call Application.Run with a
>> Worksheet.Module.Sub passed to it and also a variable number of
>> arguments. Is this possible? For example, if wishing made it so, it
>> would run like this:
>>
>> Sub app_run(macro_to_run as string, params as variant)
>> Application.Run macro_to_run, params
>> End Sub
>>
>> In my dreams, I'd call it something like this:
>>
>> Call app_run("MyBook.xls!MyModule.MyMacro", array("test 1", 1)
>>
>> And lo and behold MyMacro would run:
>>
>> Sub MyMacro(str1 as String, lng1 as Long)
>> ...
>>
>>
>> If that's not possible, is there any way to pass one string to my app_run
>> sub and have it parsed for the Workbook.Module.Macro and the args, e.g.:
>>
>> Call app_run(("MyBook.xls!MyModule.MyMacro, test1, 1")
>>
>> Thanks in Advance,
>>
>> Doug

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable number of arguments mcescher Microsoft Access VBA Modules 6 13th May 2009 05:06 PM
Variable number of arguments mcescher Microsoft Access VBA Modules 0 13th May 2009 02:57 PM
How to make delegates with variable number of arguments. LRaiz Microsoft C# .NET 4 2nd Dec 2008 04:56 AM
Passing Variable Number of Arguments to a Sub blatham Microsoft Excel Misc 4 10th Dec 2005 10:36 AM
writing a Function with a variable number of arguments TonyJeffs Microsoft Excel Programming 3 18th Jun 2004 09:01 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:09 PM.