+ Reply to Thread
Results 1 to 7 of 7

Making Calls to Global Routines

  1. #1
    Registered User
    Join Date
    07-27-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    8

    Making Calls to Global Routines

    I'm writing a program that involves a lot of the same kind of loop, just with one small variable changing each time. Is there a place where I can code the loop once, and declare a string that represents the value that changes each time, then, in a form subroutine, declare the string variable to what I want it to be, then make a call to said loop? What's the code I'll need to make a call to a loop? Say, "Col_Finder" was the loop name, and how would I get to the place where I could code that loop, and where is it?

    Thanks

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Making Calls to Global Routines

    Hi Finlay Miller,

    What you want is called passing variables and the call can be for a function or subroutine

    Cells(k, Col_Finder("Company")

    Please Login or Register  to view this content.
    or with a sub-it could be: Call Col_Finder("Company")

    Please Login or Register  to view this content.
    Last edited by xladept; 09-06-2012 at 07:09 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Making Calls to Global Routines

    Hello Finlay Miller ,

    Welcome to the Forum!

    For a Function, Sub procedure, or variable to be Global in scope (known as Public in VBA), it must be declared in a standard VBA Module in your workbook's VBA Project. In Office 2007 and 2010, you must also change your Trust setting level to allow macros to run.

    It would be better to pass the string as an argument to the Sub or Function rather than declaring it separately. It is more efficient and will remove the possibility of the global variable not containing the correct value. Here is an example that selects the first cell of the given column letter.
    Please Login or Register  to view this content.
    Changing Trust Center Settings in Excel 2010
    Last edited by Leith Ross; 09-06-2012 at 07:04 PM. Reason: Added Link
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Making Calls to Global Routines

    I think this will do the trick. If you declare your variable at the top of the module before any subroutines, it will make it globally available to every sub in that module. You then make calls to your sub that handles the loop, and pass in and out the value of the variable.
    Please Login or Register  to view this content.
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  5. #5
    Registered User
    Join Date
    07-27-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Making Calls to Global Routines

    That's great, thanks!

    One last question: What does the code in-between the brackets do?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Thanks!

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Making Calls to Global Routines

    Between the brackets are the arguments (just one argument here) - it's called passing variables, and it lets the routine know what it's working on.

  7. #7
    Registered User
    Join Date
    07-27-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Making Calls to Global Routines

    Thank you all so much, this really helped!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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