Collection as parameter

S

stefantem

Problem:
In UserForm1 I have TextBox1, TextBox2, CommandButton1 and
CommandButton2.

Private Sub CommandButton1_Click()
Call test(1)
End Sub

Private Sub CommandButton2_Click()
Call test(2)
End Sub

In Module1 I declare two collections:

dim group1, group2 as New Collection

Public Sub test (a As Integer)
group1.Add UserForm1.TextBox1, "g1"
group2.Add UserForm1.TextBox2, "g2"

''I want to put in TextBox1 or TextBox2 value 1
''and I want to use collection of controls

group & i (1).Value="1" "is not correct but I need a solution for that

End Sub
 
J

JE McGimpsey

First, you should never Dim something as a New object. See:

http://cpearson.com/excel/variables.htm

Second, your statement "I declare two collections" is wrong. Your Dim
statement only declares group2 as a collection, group1 is a Variant (see
the same reference).

One way to accomplish your desired method would be to make a collection
of your collections:

Dim group1 As Collection
Dim group2 As Collection

Public Sub test(a As Integer)
Dim groups As Collection
Dim i As Long
Set group1 = New Collection
Set group2 = New Collection
Set groups = New Collection
groups.Add group1
groups.Add group2
group1.Add UserForm1.TextBox1, "g1"
group2.Add UserForm1.TextBox2, "g2"
For i = 1 To groups.count
groups(i)(1).Text = i
Next i
End Sub

Note: I'm not sure what you're trying to do with your argument (a) for
test().
 
T

Tom Ogilvy

What is the point of having a collection with one member?

Seems like you need a better explantion of what you want to do. Right now
it appears you only need one collection and I previously gave you an example
for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top