+ Reply to Thread
Results 1 to 9 of 9

Functions, procedures and sub procedures - when to choose what?

  1. #1
    Registered User
    Join Date
    05-31-2009
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    53

    Functions, procedures and sub procedures - when to choose what?

    Ok, so I decided to learn VBA and have been looking through an online tutorial today. In this tutorial it didn't give a clear reason for when to select which of these ("a sub procedure is like a function except it cannot return values" - ok, so why use it, then?).

    While trying to understand this well enough to be able to ask a not completely retarded question I think I've picked up that both function and sub are procedures. Ok, so at least now we're down to two. But WHY would one choose a sub over a function if a function can do everything a sub can do, only better?

    Cheers!

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Functions, procedures and sub procedures - when to choose what?

    Cos programmers mainly use functions within subroutines.

    eg, you have a function that polls a webpage and returns a value, it's 25 lines of code long. You're going to use this section of code a lot in various different subroutines in your project so you've created a function that you can call from any sub at any time with 1 line of code rather than 25.

  3. #3
    Registered User
    Join Date
    05-31-2009
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Functions, procedures and sub procedures - when to choose what?

    Quote Originally Posted by BellyGas View Post
    Cos programmers mainly use functions within subroutines.

    eg, you have a function that polls a webpage and returns a value, it's 25 lines of code long. You're going to use this section of code a lot in various different subroutines in your project so you've created a function that you can call from any sub at any time with 1 line of code rather than 25.
    Ok? But a sub would only be good if you don't want something returned, right? Say I want to grab something from a table, do magic on it and insert it into another table - that would work for a sub? Is it so that only in the cases where I need something like =doSomething(c1) in a cell a sub is preferred? But is there a reason not to use a function in place of a sub?

  4. #4
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Functions, procedures and sub procedures - when to choose what?

    The way I see it, a sub procedure allows you more interaction with Excel - e.g. you can format* cells in a Sub, not in a Function. A Sub can do copy and pastes, and function cannot.

    Additionally, a sub procedure can be made to return values. Once you learn about passing variables by reference or by values (ByRef / ByVal), values changed in a subsequent procedure can be "passed" back to the main sub. For example:

    Please Login or Register  to view this content.
    Test2 effectively takes the variable x, doubles it, and "returns" it to Test1. The Debug.Print will show 10.

    In fact, you would most likely be playing around with a Sub more than a Function

    Added Disclaimer: A function CAN be made to perform actions on Excel - but that just complicates matter.
    Last edited by quekbc; 09-01-2015 at 08:18 AM. Reason: Added disclaimer

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Functions, procedures and sub procedures - when to choose what?

    @quekbc
    a sub procedure allows you more interaction with Excel - e.g. you can format cells in a Sub, not in a Function. A Sub can do copy and pastes, and function cannot.
    That's not strictly true (well it's a bit misleading anyway), one can do all those things in a Function, just not when called from a worksheet function.

    Put simply if you want to return something, use a function, if you don't then use a Sub

  6. #6
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: Functions, procedures and sub procedures - when to choose what?

    Quote Originally Posted by Kyle123 View Post
    That's not strictly true (well it's a bit misleading anyway), one can do all those things in a Function, just not when called from a worksheet function.
    Thanks. I noticed that after I posted the reply, hence an added disclaimer - didn't want to over-complicate matters but I guess I inadvertently did.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Functions, procedures and sub procedures - when to choose what?

    I reckon so, changing stuff on a worksheet is just as simple in a function as a sub

  8. #8
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Functions, procedures and sub procedures - when to choose what?

    Just to complicate it further, you can return values from a sub as well. Whoever told the OP you can't is just plain wrong.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Functions, procedures and sub procedures - when to choose what?

    @BellyGas

    Discounting ByRef (since it's not really returning), how does one return a value from a sub?

+ 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] Variable With Value For All Procedures
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2015, 10:48 AM
  2. How to use the same variable in different procedures
    By lubbamkt in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-20-2014, 02:07 AM
  3. Help calling functions to sub procedures
    By xXNetRavenXx in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-27-2010, 01:47 PM
  4. Event Procedures
    By pflipper in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-30-2009, 01:42 PM
  5. VBA Sub Procedures
    By ladeda063610 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2008, 03:12 AM
  6. Get VBA procedures from DB
    By Hiran de Silva in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2006, 12:50 PM
  7. Calling procedures from within VBA
    By Alex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2006, 08:50 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