Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Returning a string from a function

Can anyone help. I'm trying to get a year returned from a function bu
it keeps coming back empty.

I'm calling it, sending a string... in this case '2004'

Call messageYearResponse(yearResponse)

Then the function is supposed to return xYear as 2004... instead it'
coming back empty.

Private Function messageYearResponse(ByVal yearResponse As String)
Dim xYear

If yearResponse = "" Then
Exit Function
Else
If IsNumeric(yearResponse) Then
xYear = CInt(yearResponse)
Else
msgbox ("Must be numeric"), vbOKOnly
Resume
End If
End If

End Functio

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Returning a string from a function


To return a value from a function, you need to set the
name of the function to the value. Add in this line:

messageYearResponse = xYear

just before the End Function line.

HTH
Helen

-----Original Message-----
Can anyone help. I'm trying to get a year returned from

a function but
it keeps coming back empty.

I'm calling it, sending a string... in this case '2004'

Call messageYearResponse(yearResponse)

Then the function is supposed to return xYear as 2004...

instead it's
coming back empty.

Private Function messageYearResponse(ByVal yearResponse

As String)
Dim xYear

If yearResponse = "" Then
Exit Function
Else
If IsNumeric(yearResponse) Then
xYear = CInt(yearResponse)
Else
msgbox ("Must be numeric"), vbOKOnly
Resume
End If
End If

End Function


---
Message posted from http://www.ExcelForum.com/

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Returning a string from a function

Thanks Helen. I've put that line in but how do I use the returned value
when I leave the function?


---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Returning a string from a function

fergusor,
You either need to supply a return value for your function:
Private Function messageYearResponse(ByVal yearResponse As String) As
Integer
'Also, assign the return value:
messageYearResponse=MyReturnValue
End Function

or pass the input ByRef and use a sub
Private Sub messageYearResponse(ByVal yearResponse As String, ByRef myOutput
as Integer)

NickHK

"fergusor " wrote in message
...
Can anyone help. I'm trying to get a year returned from a function but
it keeps coming back empty.

I'm calling it, sending a string... in this case '2004'

Call messageYearResponse(yearResponse)

Then the function is supposed to return xYear as 2004... instead it's
coming back empty.

Private Function messageYearResponse(ByVal yearResponse As String)
Dim xYear

If yearResponse = "" Then
Exit Function
Else
If IsNumeric(yearResponse) Then
xYear = CInt(yearResponse)
Else
msgbox ("Must be numeric"), vbOKOnly
Resume
End If
End If

End Function


---
Message posted from http://www.ExcelForum.com/



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Returning a string from a function

Many thanks for the help guys, all sorted now :)


---
Message posted from http://www.ExcelForum.com/



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Returning a string from a function

fergusor,
Something like:
Dim MyReturnValue as integer

MyReturnValue =messageYearResponse(yearResponse)

If MyReturnValue=False Then 'As 0=False
MsgBox "Must be ....."
Else
'Process.....

End If

Private Function messageYearResponse(ByVal yearResponse As String) As
Integer
Dim RetVal as integer

If yearResponse is a year then
RetVal = convert to integer(yearResponse)
else
RetVal=0
end if

messageYearResponse=RetVal

End Function

You may want to read up on the limitations of the IsNumeric function to see
if it will handle all inputs. Some error handling would be a good idea also.

NickHK


"fergusor " wrote in message
...
Thanks Helen. I've put that line in but how do I use the returned value
when I leave the function?


---
Message posted from http://www.ExcelForum.com/



"Nick Cranham" wrote in message
...
fergusor,
You either need to supply a return value for your function:
Private Function messageYearResponse(ByVal yearResponse As String) As
Integer
'Also, assign the return value:
messageYearResponse=MyReturnValue
End Function

or pass the input ByRef and use a sub
Private Sub messageYearResponse(ByVal yearResponse As String, ByRef

myOutput
as Integer)

NickHK

"fergusor " wrote in message
...
Can anyone help. I'm trying to get a year returned from a function but
it keeps coming back empty.

I'm calling it, sending a string... in this case '2004'

Call messageYearResponse(yearResponse)

Then the function is supposed to return xYear as 2004... instead it's
coming back empty.

Private Function messageYearResponse(ByVal yearResponse As String)
Dim xYear

If yearResponse = "" Then
Exit Function
Else
If IsNumeric(yearResponse) Then
xYear = CInt(yearResponse)
Else
msgbox ("Must be numeric"), vbOKOnly
Resume
End If
End If

End Function


---
Message posted from http://www.ExcelForum.com/





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
Returning a text string after a 2 criteria search Spag Excel Discussion (Misc queries) 4 July 11th 08 01:49 PM
Searching in another workbook for a string and returning with the contents of the cell next to it. [email protected] Excel Worksheet Functions 1 January 12th 07 07:50 PM
Returning a string of values in an IF statement Dave F Excel Discussion (Misc queries) 1 January 11th 07 01:33 AM
returning a value from a string in a single cell AG Excel Worksheet Functions 14 August 26th 06 02:10 AM
Fix InputBox - Returning Empty String spurtniq[_3_] Excel Programming 7 December 31st 03 06:23 PM


All times are GMT +1. The time now is 03:24 AM.

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"