ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reference to Item in Collection (https://www.excelbanter.com/excel-programming/353736-reference-item-collection.html)

chad

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




Chip Pearson

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" wrote in message
...
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






chad

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" wrote in message
...
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








All times are GMT +1. The time now is 11:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com