Difference of Function and Sub

来源:互联网 发布:国外即时通讯软件排名 编辑:程序博客网 时间:2024/05/23 22:00
 

The easiest way to think of a sub vs a function is that a function can return a value and a sub cannot.  

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 is similar 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.  

 

 

 

原创粉丝点击