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 Multiple Arguments from an Excel VBA Function


Hi Again.
I had such a good response last time I thought I would ask you gurus
another one.

I have written some VBA code to manipulate some strings in a
spreadsheet.
It works well but is getting quite long, and I am beginning to reuse
parts of it. So I thought I would break one part into a function.

I would like to pass 1 string argument to my function, perform some
fairly intensive operations on the string and return with 2 string
arguments.
I think its possible to return with two argumnets - but the syntax has
thrown me.

I think it has something to do with dimensioning the Function name as a
Variant or a 2 element array ?

Here's a rough example along the lines of what I want:

Sub Blah()

MyString = "123456"
AddressSplit (MyString)

End Sub

Function AddressSplit (TargetString)
AddressSplit(1) = "Test1"
AddressSplit(2) = "Test2"

End Function


How do I return the 2 arguments to the Main Subroutine so I can use
them ?
Thanks in Advance for any help.


--
aussie_craig
------------------------------------------------------------------------
aussie_craig's Profile: http://www.excelforum.com/member.php...o&userid=15491
View this thread: http://www.excelforum.com/showthread...hreadid=271071

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 575
Default Returning Multiple Arguments from an Excel VBA Function

Craig,

It's probably more sensible to use a sub. e.g.

Sub Test()
Dim strFirstName As String
Dim strLastName As String
SplitExample "Robin Hammond", strFirstName, strLastName
MsgBox "First Name: " & strFirstName
MsgBox "Last Name: " & strLastName
End Sub

Sub SplitExample(ByVal strInput As String, ByRef strFirstName As String, _
ByRef strLastName As String)
strFirstName = Left(strInput, InStr(strInput, " ") - 1)
strLastName = Mid(strInput, InStr(strInput, " ") + 1)
End Sub

Robin Hammond
www.enhanceddatasystems.com

"aussie_craig" wrote in message
...

Hi Again.
I had such a good response last time I thought I would ask you gurus
another one.

I have written some VBA code to manipulate some strings in a
spreadsheet.
It works well but is getting quite long, and I am beginning to reuse
parts of it. So I thought I would break one part into a function.

I would like to pass 1 string argument to my function, perform some
fairly intensive operations on the string and return with 2 string
arguments.
I think its possible to return with two argumnets - but the syntax has
thrown me.

I think it has something to do with dimensioning the Function name as a
Variant or a 2 element array ?

Here's a rough example along the lines of what I want:

Sub Blah()

MyString = "123456"
AddressSplit (MyString)

End Sub

Function AddressSplit (TargetString)
AddressSplit(1) = "Test1"
AddressSplit(2) = "Test2"

End Function


How do I return the 2 arguments to the Main Subroutine so I can use
them ?
Thanks in Advance for any help.


--
aussie_craig
------------------------------------------------------------------------
aussie_craig's Profile:
http://www.excelforum.com/member.php...o&userid=15491
View this thread: http://www.excelforum.com/showthread...hreadid=271071



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Returning Multiple Arguments from an Excel VBA Function

Craig,
You can return an array from a function:

Private Sub CommandButton1_Click()
MsgBox AddressSplit ("qwerty")(0)
End Sub

Private Function AddressSplit (argStr As String) As String()
Dim RetArr(1) As String

RetArr(0) = Left(argStr, 3)
RetArr(1) = Mid(argStr, 4)
AddressSplit = RetArr
End Function



"aussie_craig" wrote in message
...

Hi Again.
I had such a good response last time I thought I would ask you gurus
another one.

I have written some VBA code to manipulate some strings in a
spreadsheet.
It works well but is getting quite long, and I am beginning to reuse
parts of it. So I thought I would break one part into a function.

I would like to pass 1 string argument to my function, perform some
fairly intensive operations on the string and return with 2 string
arguments.
I think its possible to return with two argumnets - but the syntax has
thrown me.

I think it has something to do with dimensioning the Function name as a
Variant or a 2 element array ?

Here's a rough example along the lines of what I want:

Sub Blah()

MyString = "123456"
AddressSplit (MyString)

End Sub

Function AddressSplit (TargetString)
AddressSplit(1) = "Test1"
AddressSplit(2) = "Test2"

End Function


How do I return the 2 arguments to the Main Subroutine so I can use
them ?
Thanks in Advance for any help.


--
aussie_craig
------------------------------------------------------------------------
aussie_craig's Profile:

http://www.excelforum.com/member.php...o&userid=15491
View this thread: http://www.excelforum.com/showthread...hreadid=271071



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
VLOOKUP Function returning multiple values in a separate table CAT Excel Worksheet Functions 8 October 1st 08 02:53 PM
i need help with a function application with multiple arguments tarabull Excel Worksheet Functions 3 November 3rd 07 03:36 AM
Using dates as function arguments in Excel Anthony D Excel Worksheet Functions 1 September 23rd 06 02:37 PM
VBA function returning multiple values? DM Excel Programming 9 January 15th 04 05:02 PM
Returning single/multiple values from array function Ryan Poth[_2_] Excel Programming 4 July 16th 03 08:06 AM


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