PDA

View Full Version : Solved: Many comboboxes with one name



Edoloto
10-21-2008, 11:05 AM
Greetings!

I have a problem ... I'm creating a program that needs to load 60 comboxes with certain data. Actally, this data is coming from an array, and it works well when I load 1 combo box.

My question is how can I utilize the object-oriented capabilities of vba to have all the comboboxes just going to one sub routine?

In other words, I would like to have a generic name for all the combo boxes, so they all go to the same sub routine instead of going to 60 dofferent ones.

Thank you very much!


Eduardo :banghead:

Bob Phillips
10-21-2008, 11:30 AM
Create a class called clsUserFormEvents with this code



Option Explicit

Public WithEvents mComboGroup As msforms.ComboBox

Private Sub mComboGroup_Click()
MsgBox mComboGroup.Value & " has been pressed"
End Sub


and then add this to your vform




Dim mcolEvents As Collection

Private cCboEvents As clsUserFormEvents

Private Sub UserForm_Initialize()
Dim ctl As msforms.Control

Set mcolEvents = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
Set cCboEvents = New clsUserFormEvents
Set cCboEvents.mComboGroup = ctl
mcolEvents.Add cCboEvents
End If
Next
End Sub

Edoloto
10-21-2008, 11:39 AM
Thank you so much! I really appreciate it.

Eduardo