Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
I am new to VB programming I am used to writing functions in other languages
like Perl... ususally the last line or a return $A statement defines what is returned by the function. The few vba functions I have seen confuse me as I have no indicator of what the function will return. On this function. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
From XL/VBA Help ("Writing a Function Procedure"):
A function returns a value by assigning a value to its name in one or more statements of the procedure. So Public Function foo(byVal bar As Double) As Double foo = bar + 1 End Function In article , wbntravis wrote: I am new to VB programming I am used to writing functions in other languages like Perl... ususally the last line or a return $A statement defines what is returned by the function. The few vba functions I have seen confuse me as I have no indicator of what the function will return. On this function. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
Hi Travis:
There are two types of procedure in VBA, a function which returns a value and a subroutine which cannot return as value. Although the subroutine can modify variables by using the 'byref' (default ) variable. A subrountine is used to do a task with no return like format a report or some other task where no confirmation of success is required where as a function is a process that returns a value like len(a) A function proper in VBA is specified as : FUNCTION functionname( [byval | byval] arg1 as type1 [, [byrefsubroutine | byval] arg2 as type2.....]* ] ) as typen ' code in here functionname = somevalue end function the functionname = somevalue can be anywhere in the function and you can exit the function in the middle of the code using the exit function. In certain cicrmustances more than one variable is returned and then you need to use globals or byref (like pointers in c(++)) so I could write a function function splitinhalf(byval s as string, byref s1 as string, byref s2 as string) as boolean and this function whould split the string s in half and put the answers in s1 and s2 and return true on success and false otherwise. Here is an example : function whatnumberisit(byval n as long) as string if n=1 then whacircumstancestnumberisit="onwoulde" exit function ' this one exits here endif if n=2 then whatnumberisit="two" ' this one drops to the bottom elseif n=3 them whatnumberisit="three" exit function ' this one exits here else whatnumberisit="unknown" ' this one drops to the bottom endif ' here we exit if no exit before endif so you see that there a different ways of exiting the function. I would recommend getting a book out the library or buying a book and working through it it will help a lot more than patching Internet help together. -- Hope this helps Martin Fishlock, Bangkok, Thailand Please do not forget to rate this reply. "wbntravis" wrote: I am new to VB programming I am used to writing functions in other languages like Perl... usually the last line or a return $A statement defines what is returned by the function. The few vba functions I have seen confuse me as I have no indicator of what the function will return. On this function. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
"Martin Fishlock" wrote in message
... certain cicrmustances more than one variable is returned and then you need to use globals or byref (like pointers in c(++)) so I could write a function function splitinhalf(byval s as string, byref s1 as string, byref s2 as string) as boolean and this function whould split the string s in half and put the answers in s1 and s2 and return true on success and false otherwise. or the function could return an array |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
Thanks Martin & JE
You both approached it differently so I caught the idea. I agree on the book Martin. For now I have been using global variables for the return but that did not seem the real approach I should be taking. It was more of a quick fix. I also found this page helpful "Martin Fishlock" wrote: Hi Travis: There are two types of procedure in VBA, a function which returns a value and a subroutine which cannot return as value. Although the subroutine can modify variables by using the 'byref' (default ) variable. A subrountine is used to do a task with no return like format a report or some other task where no confirmation of success is required where as a function is a process that returns a value like len(a) A function proper in VBA is specified as : FUNCTION functionname( [byval | byval] arg1 as type1 [, [byrefsubroutine | byval] arg2 as type2.....]* ] ) as typen ' code in here functionname = somevalue end function the functionname = somevalue can be anywhere in the function and you can exit the function in the middle of the code using the exit function. In certain cicrmustances more than one variable is returned and then you need to use globals or byref (like pointers in c(++)) so I could write a function function splitinhalf(byval s as string, byref s1 as string, byref s2 as string) as boolean and this function whould split the string s in half and put the answers in s1 and s2 and return true on success and false otherwise. Here is an example : function whatnumberisit(byval n as long) as string if n=1 then whacircumstancestnumberisit="onwoulde" exit function ' this one exits here endif if n=2 then whatnumberisit="two" ' this one drops to the bottom elseif n=3 them whatnumberisit="three" exit function ' this one exits here else whatnumberisit="unknown" ' this one drops to the bottom endif ' here we exit if no exit before endif so you see that there a different ways of exiting the function. I would recommend getting a book out the library or buying a book and working through it it will help a lot more than patching Internet help together. -- Hope this helps Martin Fishlock, Bangkok, Thailand Please do not forget to rate this reply. "wbntravis" wrote: I am new to VB programming I am used to writing functions in other languages like Perl... usually the last line or a return $A statement defines what is returned by the function. The few vba functions I have seen confuse me as I have no indicator of what the function will return. On this function. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
Well I would actually prefer to return a user defined type (UDT) but I didn't
really want to bring in UDT for a discussion on functions. It is a little safer than arrays. Arrays are fine but you then have to process them and deal with limits etc. But in some situations an array would be useful like split a line of text into words. -- Hope this helps Martin Fishlock, Bangkok, Thailand Please do not forget to rate this reply. "Bob Phillips" wrote: "Martin Fishlock" wrote in message ... certain cicrmustances more than one variable is returned and then you need to use globals or byref (like pointers in c(++)) so I could write a function function splitinhalf(byval s as string, byref s1 as string, byref s2 as string) as boolean and this function whould split the string s in half and put the answers in s1 and s2 and return true on success and false otherwise. or the function could return an array |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
IMO UDTs are a good idea, but badly implemented in VB. If you are a serious
VB programmer, arrays are one of your basic tools, so handling them should be meat and drink. -- --- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Martin Fishlock" wrote in message ... Well I would actually prefer to return a user defined type (UDT) but I didn't really want to bring in UDT for a discussion on functions. It is a little safer than arrays. Arrays are fine but you then have to process them and deal with limits etc. But in some situations an array would be useful like split a line of text into words. -- Hope this helps Martin Fishlock, Bangkok, Thailand Please do not forget to rate this reply. "Bob Phillips" wrote: "Martin Fishlock" wrote in message ... certain cicrmustances more than one variable is returned and then you need to use globals or byref (like pointers in c(++)) so I could write a function function splitinhalf(byval s as string, byref s1 as string, byref s2 as string) as boolean and this function whould split the string s in half and put the answers in s1 and s2 and return true on success and false otherwise. or the function could return an array |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Functions 101 The basics
IMO UDTs are a good idea, but badly implemented in VB.
Very well put. I never use UDTs except when required by an API call. The limitations on use UDTs with Collections and For Each loops make the barely useful. I always use classes instead. A Collection or Array of Class instances is much more manageable and flexible than UDTs. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com (email address is on the web site) "Bob Phillips" wrote in message ... IMO UDTs are a good idea, but badly implemented in VB. If you are a serious VB programmer, arrays are one of your basic tools, so handling them should be meat and drink. -- --- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Martin Fishlock" wrote in message ... Well I would actually prefer to return a user defined type (UDT) but I didn't really want to bring in UDT for a discussion on functions. It is a little safer than arrays. Arrays are fine but you then have to process them and deal with limits etc. But in some situations an array would be useful like split a line of text into words. -- Hope this helps Martin Fishlock, Bangkok, Thailand Please do not forget to rate this reply. "Bob Phillips" wrote: "Martin Fishlock" wrote in message ... certain cicrmustances more than one variable is returned and then you need to use globals or byref (like pointers in c(++)) so I could write a function function splitinhalf(byval s as string, byref s1 as string, byref s2 as string) as boolean and this function whould split the string s in half and put the answers in s1 and s2 and return true on success and false otherwise. or the function could return an array |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
basics? | New Users to Excel | |||
QueryTable Basics.. | Excel Programming | |||
Hi I'm New and need help with Excel Basics | Excel Discussion (Misc queries) | |||
VBA form basics | Excel Programming | |||
Free books on Charting, Basics, Data Management, Functions; Free Add-In "Excel Usability Enhancer" | Excel Programming |