+ Reply to Thread
Results 1 to 8 of 8

is a sub able to accept more than 1 veraibale fed into it?

  1. #1
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    is a sub able to accept more than 1 veraibale fed into it?

    I have a sub that I want to run with several variables passed into it. Is that possible? So if I call my_macro(var_1) within a macro, it works fine, but my_macro(var_1, var_2, var_3) does not.

    More than one variable works if I set up my_macro as a function, but then to call it within another sub so it works, I have to set up a useless variable and then write useless_var = my_macro(var_1, var_2, var_3) - something I'd like to avoid.

    Thanks
    You either quit or become really good at it. There are no other choices.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,844

    Re: is a sub able to accept more than 1 veraibale fed into it?

    This (https://msdn.microsoft.com/en-us/lib.../gg278504.aspx ) strongly suggests that it should be able to accept more than one argument. The example given at the end even shows 2 arguments to the sample sub.

    Can you post a sample of your code where multiple arguments is not working? Can you explain in more detail what "does not work" means (syntax error, error number, wrong answer)?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: is a sub able to accept more than 1 veraibale fed into it?

    You should be able to pass multiple arguments to a sub with no problem.

    How are you calling the sub?
    If posting code please use code tags, see here.

  4. #4
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: is a sub able to accept more than 1 veraibale fed into it?

    Here's a short example:

    Please Login or Register  to view this content.

    When I run mysub_1, "mysub_2(1,2)" line turns red and then an error box pops up saying "Compile Error: Expected: ="

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: is a sub able to accept more than 1 veraibale fed into it?

    Remove the (), or use Call.


    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor luv2glyd's Avatar
    Join Date
    07-13-2008
    Location
    Seattle, WA, US
    MS-Off Ver
    Excel 2010
    Posts
    679

    Re: is a sub able to accept more than 1 veraibale fed into it?

    Aha! Ok, thank you. I've always done it the way I showed above (though with no more than one variable fed into it). Any idea why I did not have to use "Call", or drop the () ?

    Thanks

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: is a sub able to accept more than 1 veraibale fed into it?

    You were lucky and obviously never trying to pass an object variable. Using the parentheses dereferences or evaluates the argument passed.

  8. #8
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: is a sub able to accept more than 1 veraibale fed into it?

    Quote Originally Posted by luv2glyd View Post
    Aha! Ok, thank you. I've always done it the way I showed above (though with no more than one variable fed into it). Any idea why I did not have to use "Call", or drop the () ?...
    What Mr rorya said is also explained well by Mr shg here
    http://www.excelforum.com/excel-prog...ml#post4226271
    Jacky

    Note I bet in your original

    my_macro(var_1)

    suggestion, than actually when you pasted that in the VB Editor Window, then it would have changed it to include a space

    my_macro (var_1)

    which is consistant with the syntax variations suggested to you

    Please Login or Register  to view this content.
    And just to reiterate...

    doing this

    my_macro (var_1)

    or this

    Call my_macro((var_1))

    makes this

    Please Login or Register  to view this content.
    work as if you had this

    Please Login or Register  to view this content.


    or alternatively makes this

    Please Login or Register  to view this content.
    work as if you sent a number rather than Var_1 at the calling line
    Last edited by Doc.AElstein; 05-02-2016 at 08:46 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Accept Bitcoins
    By EnigmaMatter in forum Suggestions for Improvement
    Replies: 6
    Last Post: 01-29-2015, 10:17 AM
  2. Accept all changes every 5 seconds
    By puuts in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-26-2014, 10:12 AM
  3. license box accept
    By sherrysue11 in forum Excel General
    Replies: 0
    Last Post: 07-20-2006, 09:45 AM
  4. [SOLVED] Won't accept textbox3
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2006, 12:30 PM
  5. Won't accept range name
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-13-2005, 02:05 PM
  6. [SOLVED] Won't accept sheet name
    By davegb in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-17-2005, 11:06 AM
  7. Excel does not accept #VALUE! Why?
    By DCR in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-08-2005, 04:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1