LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 199
Default Function to return two values

You seem to have already solved your problem. so, you maybe don't need
this one. but just for a reference. I would use a user defined variable
instead of using global variable. Using this way, your sample code looks
like this.

Type fboo
msg As String
state As Boolean
End Type

Function fbooDataIsValid(ByVal pstrParam1 As String, _
ByVal pintParam2 As Integer) As fboo

Dim tmp As fboo

If pstrParam1 = "Hi Jim" Then
tmp.msg = "Error, my friend. You should have said Hello!."
tmp.state = False
fbooDataIsValid = tmp
Exit Function
End If

If pintParam2 < 100 Then
tmp.msg = "Error, your number is < 100."
tmp.state = False
fbooDataIsValid = tmp
Exit Function
End If

tmp.msg = "Suceed"
tmp.state = True

fbooDataIsValid = tmp

End Function

' ---
Sub TestDeJac()
Dim strText As String
Dim result As fboo

strText = "Hi Jim"
result = fbooDataIsValid(strText, 136)
If Not result.state Then
MsgBox result.msg, vbCritical, "Sofica"
Else
MsgBox "It's OK...", vbInformation, "Sofica"
End If
End Sub

keiji

Jac Tremblay wrote:
Hi Jim,
When I first read your answer, I thought that wouldn't work. But I tried it
out and found it is interesting. I changed it a bit so I could get the two
values in two separated variables. Here is what I came up with:
Function IcelandBanking(ByRef strMsg As String) As Boolean
Dim WontPay As Long
Dim NoReserves As Boolean

'Code here to calculate WontPay and NoReserves
WontPay = 1
NoReserves = False

If WontPay 0 Then
strMsg = "Small Withdrawals Only"
Else
strMsg = "You are out of luck"
End If
IcelandBanking = NoReserves
End Function
'--
Sub FinancialStatus()
Dim strText As String
Dim strTrueOrFalse As String
Dim strMsg As String
Dim strResult As String
Dim intPosVbCr As Integer
' MsgBox IcelandBanking(strText) & vbCr & strText
strResult = IcelandBanking(strText) & vbCr & strText
intPosVbCr = InStr(1, strResult, vbCr)
strTrueOrFalse = Mid(strResult, 1, intPosVbCr - 1)
strMsg = Mid(strResult, intPosVbCr + 1, Len(strResult) - intPosVbCr)
MsgBox "strTrueOrFalse = " & strTrueOrFalse & vbCrLf & _
"strMsg = " & strMsg
End Sub
It works OK, but I am not sure I will use it.
My problem is that I have to check 4 different information from a
transaction. I want to group the 4 validations in a separate procedure or
function. If an error occurs, I want to display a pertinent message and exit
the main proc. Here is another way I figured out to solve my problem:
Option Explicit
Public strMessage As String
' ---
Function fbooDataIsValid(ByVal pstrParam1 As String, _
ByVal pintParam2 As Integer) As Boolean
If pstrParam1 = "Hi Jim" Then
strMessage = "Error, my friend. You should have said Hello!."
fbooDataIsValid = False
Exit Function
End If
If pintParam2 < 100 Then
strMessage = "Error, your number is < 100."
fbooDataIsValid = False
Exit Function
End If
fbooDataIsValid = True
End Function
' ---
Sub TestDeJac()
Dim strText As String
strText = "Hi Jim"
If Not fbooDataIsValid(strText, 36) Then
MsgBox strMessage, vbCritical, "Sofica"
Exit Sub
End If
MsgBox "It's OK...", vbInformation, "Sofica"
End Sub
Thank you for your original comment and good night.



 
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
IF Function with multiple return values jerminski73 Excel Worksheet Functions 6 April 28th 07 12:16 AM
A Function to Return Top 5 Values Only Sean Excel Worksheet Functions 4 December 29th 06 12:34 PM
vlookup function return all values j2thea Excel Worksheet Functions 20 November 2nd 05 10:32 PM
Return an array of values from a function Raul Excel Programming 6 December 13th 04 07:33 PM
Can a function return two values? Anil K. Excel Programming 5 August 13th 04 01:17 PM


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

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

About Us

"It's about Microsoft Excel"