Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default return 2 or more variables from a function

I just can remember how to do the following.

I need to return 2 variables from a function so that I can use them within a
sub.
In this case 2 column numbers so that I can use them to find something.

Any help would be appreciated

regards
David

-----------
Sub Find_Columns
Dim Col1, col2 as integer

Col1 = YColumn(check)

End sub


Function YColumn(check As String)

Select Case check

Case "Vegetables"
Col1 = 2
Col2 =4

Case "Mellons"
Col1 =5
Col2 = 6

End Select

End function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default return 2 or more variables from a function


David

You cannot return more than one variable from a function.

What you can do though is return a variant containing an array. Thi
array can contain any number of values that you might want to retun.

Remember, a variant can contain any data type and that includes a
array of variants as well.

Hope that helps!


Best regards


Deepak Agarwa

--
agarwaldv
-----------------------------------------------------------------------
agarwaldvk's Profile: http://www.excelforum.com/member.php...fo&userid=1134
View this thread: http://www.excelforum.com/showthread.php?threadid=48611

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default return 2 or more variables from a function

thanks

"agarwaldvk" wrote
in message ...

David

You cannot return more than one variable from a function.

What you can do though is return a variant containing an array. This
array can contain any number of values that you might want to retun.

Remember, a variant can contain any data type and that includes an
array of variants as well.

Hope that helps!


Best regards


Deepak Agarwal


--
agarwaldvk
------------------------------------------------------------------------
agarwaldvk's Profile:
http://www.excelforum.com/member.php...o&userid=11345
View this thread: http://www.excelforum.com/showthread...hreadid=486118



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default return 2 or more variables from a function

Another approach is to dimension your function as a Boolean, but pass a
bunch of arguments by ref. These might be empty placeholders passed in, but
the function gives them values. If the function does its calculations
correctly, without error, it returns a value of True, so the calling sub
knows it can rely on the arguments passed back. These can only be used by
other VBA procedures, not in UDFs.

An overly simple example might go like this:

Sub DumbSub()
Dim i1 As Integer, i2 As Integer
Dim iMin As Integer, iMax As Integer
Dim bTest As Boolean

Set rng = ActiveSheet.Range("A1:A10")
i1 = 1
i2 = 5

bTest = GetMinMax(i1, i2, iMin, iMax)

If bTest Then
MsgBox iMax & " " & iMin
Else
MsgBox i1 & " = " & i2
End If

End Sub

Function GetMinMax(iOne As Integer, iTwo As Integer, _
iLow As Integer, iHigh As Integer) As Boolean

If iOne = iTwo Then
GetMinMax = False
Exit Function
End If

If iOne iTwo then
iHigh = iOne
iLow = iTwo
Else
iHigh = iTwo
iLow = iOne
End If

GetMinMax = True

End Function

The calling sub passes in two values to find out which is greater. It checks
for an error (i.e., the two values are equal). If there's an error, it
displays one message, but if there's no error, it displays a different
message.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______


"David Adamson" wrote in message
...
thanks

"agarwaldvk"
wrote in message
...

David

You cannot return more than one variable from a function.

What you can do though is return a variant containing an array. This
array can contain any number of values that you might want to retun.

Remember, a variant can contain any data type and that includes an
array of variants as well.

Hope that helps!


Best regards


Deepak Agarwal


--
agarwaldvk
------------------------------------------------------------------------
agarwaldvk's Profile:
http://www.excelforum.com/member.php...o&userid=11345
View this thread:
http://www.excelforum.com/showthread...hreadid=486118





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
Multiple variables to sort and sum, return values<0 with sum refer Stinky Excel Worksheet Functions 5 April 20th 10 04:18 PM
Lookup function for two variables and return another cell? dcw0405 Excel Worksheet Functions 2 February 4th 10 05:23 PM
using =IF function with 12 variables swell estimator[_2_] Excel Discussion (Misc queries) 4 September 8th 08 12:09 AM
Lookup based on two variables and return a third Mark Excel Worksheet Functions 3 August 29th 08 03:26 PM
2 Conditions True, Many Variables, Return Result elliekev26 Excel Worksheet Functions 4 September 22nd 05 04:16 PM


All times are GMT +1. The time now is 01:36 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"