Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default Can VBA function be used to return value

Hello,
Can a VBA function be used to return a value, such as in C? I would
like a function to do the following:

output_value = function_name(input_value)

Can someone please send me a brief example of this?

thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Can VBA function be used to return value

Copy the below function to VBA module and try the formula from a cell

A1 = "asdf"
B1 = ReverseString(A1)

Function ReverseString(varTemp) As String
ReverseString = StrReverse(varTemp)
End Function



--
If this post helps click Yes
---------------
Jacob Skaria


"Andrew" wrote:

Hello,
Can a VBA function be used to return a value, such as in C? I would
like a function to do the following:

output_value = function_name(input_value)

Can someone please send me a brief example of this?

thanks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Can VBA function be used to return value

On May 13, 7:52*am, Jacob Skaria
wrote:
Copy the below function to VBA module and try the formula from a cell

A1 = "asdf"
B1 = ReverseString(A1)

Function ReverseString(varTemp) As String
ReverseString = StrReverse(varTemp)
End Function

--
If this post helps click Yes
---------------
Jacob Skaria

"Andrew" wrote:
Hello,
Can a VBA function be used to return a value, such as in C? *I would
like a function to do the following:


output_value = function_name(input_value)


Can someone please send me a brief example of this?


thanks


This isn't exactly what I meant, although it will work. Here's an
example which isn't really code, but it describes how this function
should work.

Sub function1()
dim k as integer
dim q as integer
cells(1,1)=k
q=function2(k) ' let variable q contain the return value of function2
End sub

Sub function2(ByVal n as integer)
n=n+1

return n
End Sub
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Can VBA function be used to return value

Because you Dim'med the 'q' variable inside function1, it will not be able
to be seen outside of the procedure. The way to return a value from a
function in VB is to assign the value to the function name within the
function itself; although, for simple functions, you can treat the function
name as if it were a declared variable (and it is also a good idea to
specify the return type in the function's procedure header). You are
probably looking for something like this...

Sub Function1() As Long
Dim k As Long
k = Cells(1,1).Value
Function1 = Function2(k)
End sub

Sub Function2(ByVal n as Long) As Long
Function2 = Function2 + 1
End Sub

--
Rick (MVP - Excel)


"Andy" wrote in message
...
On May 13, 7:52 am, Jacob Skaria
wrote:
Copy the below function to VBA module and try the formula from a cell

A1 = "asdf"
B1 = ReverseString(A1)

Function ReverseString(varTemp) As String
ReverseString = StrReverse(varTemp)
End Function

--
If this post helps click Yes
---------------
Jacob Skaria

"Andrew" wrote:
Hello,
Can a VBA function be used to return a value, such as in C? I would
like a function to do the following:


output_value = function_name(input_value)


Can someone please send me a brief example of this?


thanks


This isn't exactly what I meant, although it will work. Here's an
example which isn't really code, but it describes how this function
should work.

Sub function1()
dim k as integer
dim q as integer
cells(1,1)=k
q=function2(k) ' let variable q contain the return value of function2
End sub

Sub function2(ByVal n as integer)
n=n+1

return n
End Sub

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
Return value for a function. Jeff Excel Discussion (Misc queries) 1 June 2nd 08 09:08 PM
How to return a value from a function? Sing Excel Programming 3 September 22nd 07 05:46 PM
Why does this function NOT return a value? Nooby Excel Discussion (Misc queries) 3 May 22nd 06 03:07 PM
Only do Sum function if not "0", IF"0" then return "0" Mike Punko Excel Worksheet Functions 1 June 1st 05 12:20 AM
Function to return value from each tab Steven Leuck Excel Worksheet Functions 1 March 3rd 05 04:11 AM


All times are GMT +1. The time now is 01:06 PM.

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

About Us

"It's about Microsoft Excel"