View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Passing Variable Number of Arguments to a Sub

How about just passing that array to the second sub.

Here's an example that passes an array, a range and a string to the second sub:

Option Explicit
Sub One()

Dim sArgs() As String
Dim iCtr As Long

ReDim Preserve sArgs(1 To 3)
For iCtr = LBound(sArgs) To UBound(sArgs)
sArgs(iCtr) = iCtr & "--A"
Next iCtr

Call sub2(sArgs, ActiveSheet.Range("a1"), "test33")

End Sub


Sub sub2(ParamArray Args())

Dim myElement As Variant
Dim iCtr As Long
Dim myCell As Range

For Each myElement In Args
If IsArray(myElement) Then
For iCtr = LBound(myElement) To UBound(myElement)
MsgBox myElement(iCtr)
Next iCtr
ElseIf TypeOf myElement Is Range Then
For Each myCell In myElement.Cells
MsgBox myCell.Value
Next myCell
ElseIf VarType(myElement) = vbString Then
MsgBox myElement
Else
'do nothing
End If
Next myElement

End Sub

blatham wrote:

Does anyone know how I can do the above.

I am constructing a list of arguments in one sub to pass to another.
Something along the following lines:

Sub One

Dim sArgs() As String
j = 0
ReDim Preserve sArgs(j)
sArgs(j) = Chr(34) & "Arg1" & chr(34)

j = 1
ReDim Preserve sArgs(j)
sArgs(j) = Chr(34) & "Arg2" & chr(34)

Call Sub2(Join( Args(), ",")

End Sub

Sub2(ParamArray Args())

'Sub 2 Code

End Sub

I know the problem lies in the fact that the join statement returns a
single string. I want to know how I can have the strings separated by
comma recogonised as separate arguments for Sub 2 to accept.

--
blatham
------------------------------------------------------------------------
blatham's Profile: http://www.excelforum.com/member.php...o&userid=19441
View this thread: http://www.excelforum.com/showthread...hreadid=491996


--

Dave Peterson