Need help creating a Collection of Combo/Text Boxes on a userform

Grib30

New Member
Joined
Feb 5, 2016
Messages
4
Hi all, I have a userform which is being built as a calulation tool

I have a combo boxes and text boxes that are dependant on each other, once filled in there is a button that should calculate a range of values dependant on
1.) The value in the text box
2.) The selection in the corresponding combo box
There are 36 of each type in total

I have been looking at a number of potential methods including arrays and loops but a collection seems to be the only one that looks like it may work, only trouble is I can't get my head around how to set it up using the examples I've seen so far online.

I could write it out as below, but it would be a nightmare should I need to change anything etc
Code:
Private Sub cbAdd_Click()
    With lbHP                                           'label name where total will be displayed
        lbHP.Caption = 0                                'set label value to 0
        Call chkAtt(lbHP, cbS1MA, tbS1MA, "HP")         'Call function to check if the attribute in each combo box
        Call chkAtt(lbHP, cbS2MA, tbS2MA, "HP")         'matches the label type, and if so add the value of the corresponding
        Call chkAtt(lbHP, cbS3MA, tbS3MA, "HP")         'text box
        Call chkAtt(lbHP, cbS4MA, tbS4MA, "HP")
        Call chkAtt(lbHP, cbS5MA, tbS5MA, "HP")
        Call chkAtt(lbHP, cbS6MA, tbS6MA, "HP")
    End With
    
End Sub
Function chkAtt(ByVal myLB As Object, myCB As Object, myTB As Object, myTest As String)
        If myCB = myTest Then
            myLB.Caption = CLng(myLB.Caption) + CLng(myTB)
        Else
        End If
End Function

If I could add all these combo boxes and text boxes to a collection, then I should be able to loop through them instead of having to type out the 'Call chkAtt()' for each combo box, assuming this is correct I just need to know how to
1.)Create said collection(s)?
2.)Add the objects
3.)How to loop through the items and update the label with the total (I could work this out myself I'm sure but it's Friday afternoon and my head is sore after trying to understand collections all day -_-)



If more info required please ask, I don't always explain specifics very well ><

(or if it can be done another way I'm open to that too!)

Thanks in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
It's not really clear what you want to do and why you want/need a collection.

If you just want to be able to loop through the controls on the userform perhaps you could do something with the existing Controls collection of the userform.
 
Upvote 0
There are a number of other controls on the form that I don't need to evaluate.

There are:
8 labels
36 combo boxes
36 text boxes

On the click of a button I require the following:
8 labels to show the totals from the 36 text boxes depending on what is selected in the corresponding combo box

- 36 combos boxes are populated with strings relating to the 8 labels
- 36 text boxes will contain a numericaly value that relates to the selection in the combo box
- I only want 'label 1' to sum the totals of the text boxes whos relative combo box has 'label 1' selected from the drop down


I don't know how to create the collection(s) I need in order to do the above


Like I mentioned, I've tried other methods(arrays mainly) but from what I understand when googling, a collection seem's to be needed so I can reference the combo/text boxes. If it can be done another way great

This any clearer?
 
Upvote 0
Sorry to clarify, there are a number of other text/combo boxes

The ones listed above are the objects that are involved
 
Upvote 0
Have you looked at using the Controls collection?

Perhaps that combined with some sort of naming convention could work.
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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