Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Sub vs Function

Something that I've always been curious about, but never found a real reason
behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well never be
used.

Thoughts? Comments?


--
Rob van Gelder - http://www.vangelder.co.nz/excel



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Sub vs Function

Sub has no return value whereas Function can return object or variable. You must decide to use Sub or Function according to what you want to do
Valid déclaration of function :
Function Test() AS boolea
'test a value, check something or do a calculatio
Test = True 'return your test or calculatio
End Function
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Sub vs Function

I think its a question of style.

personally I like to use functions when I want to return a single value,
otherwise subs.
I find that easier to understand when I look at code I wrote a few years
ago.

regards
Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com/FxlV2WhatsNew.htm

"Rob van Gelder" wrote in message
...
Something that I've always been curious about, but never found a real

reason
behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well never be
used.

Thoughts? Comments?


--
Rob van Gelder - http://www.vangelder.co.nz/excel





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Sub vs Function

Hi Rob
one difference between sub and functions:
Function can not change the Excel environment. That is they can't
change formats, other cells, etc. Only subs can do this.

Also have a look at http://www.cpearson.com/excel/differen.htm


--
Regards
Frank Kabel
Frankfurt, Germany


Rob van Gelder wrote:
Something that I've always been curious about, but never found a real
reason behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well
never be used.

Thoughts? Comments?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Sub vs Function

Frank,

This is confusing worksheet functions with VB/VBA functions. Functions can
change the attributes of the container application (such as worksheet cell
attributes) if they are not called from a worksheet function, just like a
sub.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Frank Kabel" wrote in message
...
Hi Rob
one difference between sub and functions:
Function can not change the Excel environment. That is they can't
change formats, other cells, etc. Only subs can do this.

Also have a look at http://www.cpearson.com/excel/differen.htm


--
Regards
Frank Kabel
Frankfurt, Germany


Rob van Gelder wrote:
Something that I've always been curious about, but never found a real
reason behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well
never be used.

Thoughts? Comments?






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Sub vs Function

Hi Bob
thanks for the clarification!

--
Regards
Frank Kabel
Frankfurt, Germany


Bob Phillips wrote:
Frank,

This is confusing worksheet functions with VB/VBA functions.
Functions can change the attributes of the container application
(such as worksheet cell attributes) if they are not called from a
worksheet function, just like a sub.


"Frank Kabel" wrote in message
...
Hi Rob
one difference between sub and functions:
Function can not change the Excel environment. That is they can't
change formats, other cells, etc. Only subs can do this.

Also have a look at http://www.cpearson.com/excel/differen.htm


--
Regards
Frank Kabel
Frankfurt, Germany


Rob van Gelder wrote:
Something that I've always been curious about, but never found a
real reason behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well
never be used.

Thoughts? Comments?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Sub vs Function

Function can not change the Excel environment.

Just for clarity, it should be said that a function (or sub)
called from a worksheet cell cannot change the Excel environment.
Functions, per se, can change the Excel environment. It is only
when they are called directly or indirectly from a worksheet cell
that the prohibition on changes comes in to play.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Frank Kabel" wrote in message
...
Hi Rob
one difference between sub and functions:
Function can not change the Excel environment. That is they

can't
change formats, other cells, etc. Only subs can do this.

Also have a look at http://www.cpearson.com/excel/differen.htm


--
Regards
Frank Kabel
Frankfurt, Germany


Rob van Gelder wrote:
Something that I've always been curious about, but never

found a real
reason behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as

well
never be used.

Thoughts? Comments?




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Sub vs Function

Rob,

Great question (which is usually an intro into a highly opinionated
discourse<vbg).

As has been pointed out, Function (can) return a value, but it doesn't have
to. So in your example a call to Test works in the same manner, regardless
of whether it is a Sub or a Function.

Paul Lomax, in VB & VBA In A Nutshell (O'Reilly ISBN: 1-56592-358-8)
suggests that you should never use Subs for a custom procedure, always use
Functions, precisely because Functions can return a value. At a minimum this
could be a Boolean indicating successful or unsuccessful completion of that
procedure. I find myself drawn to this argument, as I am sure that you are
like me, and omit error handling in many cases (this code will never error
so why bother - FLW). By using a function you can return the procedure
status to the caller very simply. Clearly, it gets more difficult if you
have a function that needs to return some other value.

Paul has also tested the performance of a call to a sub or a function, and
has found no discernible difference. Of course, there is often an extra
statement in a function, namely that of setting the return value.

As I say, I am drawn to Paul's argument on a theoretical basis, but I do not
rigorously adhere to it, but that is laziness or sloppiness on my part, not
for any other good, pre-determined reason.

In summary, I concur with Paul's reasoning that using Function calls makes
good sense, but apply in my own style (typical programming I guess) and for
specific purpose rather than as a blanket procedure.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rob van Gelder" wrote in message
...
Something that I've always been curious about, but never found a real

reason
behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well never be
used.

Thoughts? Comments?


--
Rob van Gelder - http://www.vangelder.co.nz/excel





  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Sub vs Function

Hi Rob

A sub is simply a function returning Void, nothing, as you know. So there
are no differences from a programming view.

In Excel, however, you'll find Subs in the Tools Macro Start menu, and
you also can assign them to buttons and objects. Not so with funtions, you
can't run them outside the VBE unless they are called from a Sub.

Functions can be used directly in spreadsheet cells -where they, as Frank
points out, loose their "macro power".

--
HTH. Best wishes Harald
Followup to newsgroup only please

"Rob van Gelder" skrev i melding
...
Something that I've always been curious about, but never found a real

reason
behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well never be
used.

Thoughts? Comments?


--
Rob van Gelder - http://www.vangelder.co.nz/excel





  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sub vs Function

There is another "advantage"; you can view all your function names in the
function wizard if you don't make them private.

--
Regards,
Tom Ogilvy


"Harald Staff" wrote in message
...
Hi Rob

A sub is simply a function returning Void, nothing, as you know. So there
are no differences from a programming view.

In Excel, however, you'll find Subs in the Tools Macro Start menu, and
you also can assign them to buttons and objects. Not so with funtions, you
can't run them outside the VBE unless they are called from a Sub.

Functions can be used directly in spreadsheet cells -where they, as Frank
points out, loose their "macro power".

--
HTH. Best wishes Harald
Followup to newsgroup only please

"Rob van Gelder" skrev i melding
...
Something that I've always been curious about, but never found a real

reason
behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well never

be
used.

Thoughts? Comments?


--
Rob van Gelder - http://www.vangelder.co.nz/excel









  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Sub vs Function

Thanks to everyone for your reply.

Just to sum up, it appears there is no performance/ functional difference
VBA-wise, but there are advantages defining a Sub or Function if it's to be
integrated into Excel.

Very useful to know that.

Cheers!

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Rob van Gelder" wrote in message
...
Something that I've always been curious about, but never found a real

reason
behind.

Why do we use a Sub instead of using a Function?

Is there any reason other than a Sub returns nothing?

These both do exactly the same thing:

Function Test()
MsgBox "test"
End Function

Sub Test()
MsgBox "test"
End Sub

At the moment my opinion is that Sub is redundant and may as well never be
used.

Thoughts? Comments?


--
Rob van Gelder - http://www.vangelder.co.nz/excel





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy of excel file not showing formulal/function in the function b oaallam Excel Discussion (Misc queries) 4 September 6th 07 01:20 PM
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
Offset function with nested match function not finding host ss. MKunert Excel Worksheet Functions 1 March 21st 06 10:46 PM
Emulate Index/Match combo function w/ VBA custom function Spencer Hutton Excel Worksheet Functions 2 May 2nd 05 05:26 PM
Adding a custom function to the default excel function list DonutDel Excel Programming 3 November 21st 03 03:41 PM


All times are GMT +1. The time now is 10:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"