Function vs. Procedure

dave8

Active Member
Joined
Jul 8, 2007
Messages
264
Can someone tell me the difference between a Function and Procedure? From what I understand is that a Function returns a value. Can a Procedure return a value? How?
 

SidBord

Active Member
Joined
Aug 23, 2004
Messages
346
The primary effect of a function is to behave like a variable, except the function must do some processing whereas a variable can’t do any processing. One other MAJOR difference is that a macro function MUST NOT do anything that changes anything on a worksheet!! A function can otherwise do just about any kind of processing that a procedure can. It MUST however execute a statement that returns a value. For example, if you write a function named “GetThisSheetName”, somewhere in the code you MUST have a statement something like this:

GetThisSheetName = “Name of Sheet”
and that statement must be executed before exiting the function. Then, if used in a worksheet formula, you might write something like:
= Call GetThisSheetName()
Note that you MUST use parentheses following a function name that’s referenced in a formula. When used in a macro, the function name does not require the parens and you don’t need the “Call”. I suggest you look at help for the rules about arguments.

A procedure can return one or more values by using arguments. Normally arguments are used to pass information “to” a macro procedure, but you can use an argument to return a value ”from” the procedure to the caller if convenient.

One other thing to consider is results of an error that occurs in a function. Since a function MUST always return a value, you should provide a returned value that tells the calling routine a disaster has occurred. If your function normally returns a positive integer as a value, then you could return -1 if an error occurs.
HTH, Sid
 

Forum statistics

Threads
1,081,441
Messages
5,358,694
Members
400,508
Latest member
fish31

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top