+ Reply to Thread
Results 1 to 6 of 6

Passing different variables back and forth between Sub and Function

  1. #1
    Registered User
    Join Date
    05-26-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    49

    Passing different variables back and forth between Sub and Function

    I have a Subroutine that Calls a Function. The Subroutine Calls a Function looking like Call RandomFunction(RandomDateVariable, RandomStringVariable) and the Subroutine passes those two variables to the Function. The Function takes it and runs with it. In return I want the RandomFunction to pass back 3 unique String variables unrelated to the RandomDateVariable and RandomStringVariable. The problem is that when we get back to the Subroutine the Function never passes the New Strings that were developed in the Function. I have searched all over Google looking for examples. In the Function each of the strings looks like String1 = Cstr(arrRandom(1)) and String2 = Cstr(arrRandom(2)) etc but the Subroutine can't see what String1, String2, String3 are. I am a relative beginner and I can't figure out. I tried declaring Public Variables and that didn't work. I tried passing the Strings in arrRandom(1) and arrRandom(2) and arrRandom(3) and I can't figure that out - says "Compile Error - Argument Not Optional" in the Subroutine - whatever that means. Any thoughts or examples is appreciated. In summation how do you pass multiple new strings built in the Function back to a Subroutine? Thank you very much.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Passing different variables back and forth between Sub and Function

    That seems an awkward method.

    Why don't you declare some public variables?

    At the top of your Subroutine sheet.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Passing different variables back and forth between Sub and Function

    Hi james,

    The following 3 examples may help (tested and working). I'm not a big fan of the last example, because I try to avoid 'Variant' types.

    Lewis

    Please Login or Register  to view this content.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Passing different variables back and forth between Sub and Function

    First of all, from your description it sounds like the Function you are calling should really be a Sub. The purpose in life of a Function is to return a value. If you are invoking it with a Call statement, then you are not doing that. So in my description below I am going to refer to them both as Subs.

    Second, using Public variables will certainly work, but this creates what is called common environment data coupling. This is a form of tight coupling that is best avoided. This was a concept first articulated by Yourdon in the late 1970s and quickly embraced by the software engineering community. There are a few reasons for this, primarily that it is difficult to diagnose bugs involving common data, and easy to introduce new bugs when new code refers to common data.

    The textbook method is to simply add three arguments to the Sub you are calling. The lower-level Sub assigns values to the three strings, and they are returned to the caller. It is important to understand that in VBA, arguments are passed by default by reference. That's why this solution works. That is, when a Sub is called, and it refers to one of its arguments, it is referring to the exact same location in memory as the variable used by the caller. (There is an option to refer to an argument by value. In this case, the lower-level Sub makes a copy of the variable and works on its copy, and doesn't touch the location owned by the caller.)

    This is sample code (compiled but not executed):

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Registered User
    Join Date
    05-26-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Passing different variables back and forth between Sub and Function

    Great stuff! Thank you for the responses. @LJMetzger I appreciate you sending me some different examples. @6StringJazzer you killed it - that was exactly what I was looking for - your explanation was superb and yes I wasn't sure whether to use Functions or Subroutines. I never knew or thought you could or should pass empty variables so that in order to get them passed back. Thank you very much! I really appreciate your time and passing knowledge to me!

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Passing different variables back and forth between Sub and Function

    You're very welcome. I'm glad you find it clear and useful.

+ 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. Passing on information to a function with no variables
    By moosemaster23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2013, 08:21 AM
  2. [SOLVED] Passing Variables from Sub to Function
    By jimdare in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-28-2012, 11:29 PM
  3. Problem with passing variables to function
    By rasx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2011, 04:44 AM
  4. Passing variables to Range function?
    By svaiskau in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2007, 11:29 AM
  5. VBA passing variables through a function
    By Jeff in forum Excel General
    Replies: 2
    Last Post: 11-03-2005, 07:30 PM

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