+ Reply to Thread
Results 1 to 3 of 3

Reference to Item in Collection

  1. #1
    Chad
    Guest

    Reference to Item in Collection

    I want to cycle through a collection and add members on the basis of whether
    they exist. For example:

    Function Test(Optional M1, Optional M2, Optional M3)
    Dim Group as New Collection

    With Group
    If Not IsMissing(M1) then .Add M1
    If Not IsMissing(M1) then .Add M1
    If Not IsMissing(M1) then .Add M1
    end with
    .. . .
    End Function

    Is it possible to build a reference such that I could use a loop:

    For i=1 to 10
    If Not IsMissing("M" & i) then Group.Add "M" & i
    next i

    Is there a function that would tell VBA to recognize the string as a
    variable (or object, etc.)? This is quite simple to do in SAS.

    Thanks,

    Chad




  2. #2
    Chip Pearson
    Guest

    Re: Reference to Item in Collection

    Chad,

    You could declare the argument to Test as a ParamArray. E.g.,

    Sub Test(ParamArray M() As Variant)
    Dim Ndx As Long
    For Ndx = LBound(M) To UBound(M)
    If IsMissing(M(Ndx)) = True Then
    Debug.Print "missing " & Ndx
    Else
    Debug.Print M(Ndx)
    End If
    Next Ndx
    End Sub


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    "Chad" <[email protected]> wrote in message
    news:[email protected]...
    >I want to cycle through a collection and add members on the
    >basis of whether
    > they exist. For example:
    >
    > Function Test(Optional M1, Optional M2, Optional M3)
    > Dim Group as New Collection
    >
    > With Group
    > If Not IsMissing(M1) then .Add M1
    > If Not IsMissing(M1) then .Add M1
    > If Not IsMissing(M1) then .Add M1
    > end with
    > . . .
    > End Function
    >
    > Is it possible to build a reference such that I could use a
    > loop:
    >
    > For i=1 to 10
    > If Not IsMissing("M" & i) then Group.Add "M" & i
    > next i
    >
    > Is there a function that would tell VBA to recognize the string
    > as a
    > variable (or object, etc.)? This is quite simple to do in SAS.
    >
    > Thanks,
    >
    > Chad
    >
    >
    >




  3. #3
    Chad
    Guest

    Re: Reference to Item in Collection

    Thanks. I think this is what I need.

    Chad

    "Chip Pearson" wrote:

    > Chad,
    >
    > You could declare the argument to Test as a ParamArray. E.g.,
    >
    > Sub Test(ParamArray M() As Variant)
    > Dim Ndx As Long
    > For Ndx = LBound(M) To UBound(M)
    > If IsMissing(M(Ndx)) = True Then
    > Debug.Print "missing " & Ndx
    > Else
    > Debug.Print M(Ndx)
    > End If
    > Next Ndx
    > End Sub
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    > "Chad" <[email protected]> wrote in message
    > news:[email protected]...
    > >I want to cycle through a collection and add members on the
    > >basis of whether
    > > they exist. For example:
    > >
    > > Function Test(Optional M1, Optional M2, Optional M3)
    > > Dim Group as New Collection
    > >
    > > With Group
    > > If Not IsMissing(M1) then .Add M1
    > > If Not IsMissing(M1) then .Add M1
    > > If Not IsMissing(M1) then .Add M1
    > > end with
    > > . . .
    > > End Function
    > >
    > > Is it possible to build a reference such that I could use a
    > > loop:
    > >
    > > For i=1 to 10
    > > If Not IsMissing("M" & i) then Group.Add "M" & i
    > > next i
    > >
    > > Is there a function that would tell VBA to recognize the string
    > > as a
    > > variable (or object, etc.)? This is quite simple to do in SAS.
    > >
    > > Thanks,
    > >
    > > Chad
    > >
    > >
    > >

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1