Friday, 30 November 2012

What is the difference between Subroutine and Function in VBA

Excel VBA Sub Routine Vs Function
The Sub(routine) can NOT return a value. But a Function can return a value. If you want to call them then a Function can be called by it’s name with variables in parenthesis and  a Subroutine can be called only by writing it name with variables without parenthesis as explained below.
Subroutine (Sub) Syntax with Example:                                                              
Sub  Add(x as integer, y as integer, z as integer)
z= x+y
End Sub
'Calling Subroutine
Add  x, y, z
when it will return result will be saved in z variable, so it has not returned anything.

Function Syntax with Example:                                                                 
Function  Add(x as integer, y as integer) as integer
Add= x+y        
End Function
'Calling Function
T=Add( x, y)
when it will return result will be stored in ‘T’ variable. The function always returns a value so its value must be saved in some variable

More Explanation:

The easiest way to think of a Sub vs a Function is that a Function can return a value and a Sub cannot. A Sub can be thought of as a small program that performs some action that is contained within the program. 
Subs can be used to update a cell or perform an import and calculation, but the result can’t be returned to another sub or function. Another thing to note is that a sub (or macro) cannot be accessed directly by a cell reference. 
For example, when a cell is used to show today’s date, =Today(), that formula is also a built-in function, not a sub.

Function :

A Function is similar to a Sub, except that a Function can return a value. It may be easier to think of a function as similar to a formula in excel. You can provide the function the necessary inputs and the function returns the desired value. 
You can build a custom function for just about any action and then access that function from either a call from a macro, or a direct reference from a cell. 



  1. If some one wishes to be updated with hottest technologies afterward he must be visit this
    web page and be up to date daily.

  2. We are a group of volunteers and starting a brand new scheme in our community.
    Your website offered us with valuable info to work on. You've done
    an impressive process and our whole neighborhood might be
    grateful to you.

  3. I don't even know how I stopped up here, however I assumed this submit was great.
    I don't recognise who you are however certainly you are going to
    a well-known blogger in the event you aren't already.

  4. You really make it appear so easy together with your presentation however I find this topic to be actually one thing that I think I might never understand.
    It kind of feels too complicated and extremely
    broad for me. I am having a look forward to your subsequent submit,
    I will attempt to get the hang of it!

  5. I was very happy to uncover this website. I want to to thank you for your time due to this wonderful read!!
    I definitely appreciated every little bit
    of it and I have you saved to fav to look at new things on your site.

  6. Good post. I certainly appreciate this site. Thanks!

  7. I get pleasure from, cause I found just what I used to be
    taking a look for. You've ended my 4 day lengthy hunt!
    God Bless you man. Have a nice day. Bye

  8. Hello, after reading this amazing paragraph i
    am too glad to share my experience here with colleagues.

  9. Greate post. Keep writing such kind of information on your blog.
    Im really impressed by your site.
    Hey there, You have performed an incredible job. I will definitely digg it and personally recommend
    to my friends. I am confident they will be benefited from this website.

  10. Peculiar article, exactly what I wanted to find.

  11. I was recommended this blog by my cousin. I'm not sure whether this post is written by
    him as nobody else know such detailed about my trouble.
    You are amazing! Thanks!

  12. There is definately a lot to learn about this issue. I love all the points you have made.

  13. Appreciating the persistence you put into your blog and detailed information you present.
    It's great to come across a blog every once in a while that isn't the same outdated rehashed material.

    Wonderful read! I've saved your site and I'm
    including your RSS feeds to my Google account.

  14. Great blog here! Also your website loads up very fast!
    What host are you using? Can I get your affiliate link to your
    host? I wish my website loaded up as fast as yours lol

  15. Hello! Someone in my Myspace group shared this site with us so I
    came to look it over. I'm definitely loving the information. I'm book-marking and will be tweeting this to my followers!
    Fantastic blog and excellent design and style.


Hi User, Thank You for visiting My Blog. Please post your open Feedback only related to this Blog Posts. Please note that I cannot respond to the Anonymous Comments.

Subscribe to Blog Posts by Email

ExcelKingdom-Popular Posts