PC Review


Reply
Thread Tools Rate Thread

Can VBA function be used to return value

 
 
Andrew
Guest
Posts: n/a
 
      13th May 2009
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
 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      13th May 2009
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
>

 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      13th May 2009
On May 13, 7:52*am, Jacob Skaria
<JacobSka...@discussions.microsoft.com> 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 With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      13th May 2009
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" <(E-Mail Removed)> wrote in message
news:45c9444f-f644-4f22-9ce8-(E-Mail Removed)...
On May 13, 7:52 am, Jacob Skaria
<JacobSka...@discussions.microsoft.com> 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Public function used to return Criterion to Query - return "*" =?Utf-8?B?YWxleGhhdHppc2F2YXM=?= Microsoft Access VBA Modules 4 6th Aug 2005 03:01 AM
Return a ref from a function =?Utf-8?B?QXJuZQ==?= Microsoft C# .NET 2 6th Jun 2005 03:05 PM
Can a function return more than one value ? =?Utf-8?B?bXNjZXJ0aWZpZWQ=?= Microsoft Access Getting Started 1 9th Mar 2005 06:21 PM
Function to return value from each tab =?Utf-8?B?U3RldmVuIExldWNr?= Microsoft Excel Worksheet Functions 1 3rd Mar 2005 04:11 AM
Trap error in sub or function, return sub or function name? Tim Frawley Microsoft VB .NET 6 16th Mar 2004 01:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:27 PM.