Adding Data to a Collection

kingofaces

Board Regular
Joined
Aug 23, 2010
Messages
68
Using collections is something I picked upon just recently, so I'm having a little trouble understanding why the current function returns a #Name error. Basically what I'm trying to do is add the y1 and y2 variables into a collection called ygroup. I'm then using a For Each loop to do a calculation for each of the y variables within y group. This is an example I have been tinkering with to try and understand how I could use collections in some of my larger projects, so any insight would go a long way. I am a bit of a beginner when it comes to collections and arrays, so maybe there is a much simpler solution if this isn't the most feasible method?

Code:
Function test(x, y1, y2) As Long
Dim ygroup As New Collection
Dim y As Long
test1 As Long

With ygroup
.Add Item:=y1
.Add Item:=y2
End With


For Each y In ygroup
Do
test1 = (x + y)
test = test + test1
Next y


End Function

I get the feeling I've just misunderstood how to properly enter the data into a collection after Googling various examples which didn't seem to explain the process very well for me.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
Sub Mytest()
    MsgBox fxTest(1, 2, 3)
End Sub

Function fxTest(x As Long, y1 As Long, y2 As Long) As Long

    Dim ygroup As New Collection
    Dim y As Variant
    
    With ygroup
        .Add Item:=y1
        .Add Item:=y2
    End With
    
    For Each y In ygroup
        fxTest = fxTest + (x + y)
    Next y

End Function
 
Upvote 0
Thanks Alphafrog, that did the trick. At first it still returned an error until I realized I had code open for just the sheet that I had been working with, but when I put it into a module the function worked fine. At least that clarifies I was on the right track and just missed a little housekeeping issue. The site Bill provided seemed like a good one when I first found it, but even though it seemed straightforward I kept searching when I couldn't find what was causing the issue. Thanks for showing me I'm going the right direction with this at least folks.

Ruddles, the test variable is initially 0 or nothing (pretty sure such as a statement isn't required aside from letting others know what is). All that value was doing was keeping a running total of each time the For Each loop did a calculation.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top