How to associate userform controls with each other?

jplank

Board Regular
Joined
Sep 19, 2012
Messages
62
This is an oversimplified version of what I am trying to do. I have a userform with 10 textboxes and 10 comboboxes. The textboxes are named Text1, Text2, ..., Text10. The comboboxes are named Combo1, Combo2, ..., Combo10. For each of the textboxes, when a value is input, I want the rowsource of the associated combobox to change. For example:

VBA Code:
Private Sub Text1_Change()
Combo1.RowSource = Text1.value
End Sub

Private Sub Text2_Change()
Combo2.RowSource = Text2.value
End Sub

...etc
I have similar code for Text3_Change, ..., Text10_Change. Again, this is a simplification and the code is slightly more lengthy/complex. I'm wondering if there's an easier way to set this up so that the code isn't so repetitive. Is there a way to associate each textbox and combobox together so that I can write a single piece of code that says "when any textbox is updated, set the rowsource of the associated combobox to the value of it's textbox." I hope I'm articulating that well enough. I feel like this ought to be possible, but not sure how to do it.

I have crossposted this question here
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You could use a class to handle the Change event of each textbox.

1 Create a new class module, name it CTextBoxes and paste this code in it.
Code:
Option Explicit

Public WithEvents TBox As MSForms.TextBox

Private Sub TBox_Change()
    TBox.Parent.Controls(TBox.Tag).RowSource = TBox.Value
End Sub

2 Add this code to the userform module.

Code:
Option Explicit
Dim TextBoxes() As CTextBoxes

Private Sub UserForm_Initialize()
Dim idx As Long

    ReDim TextBoxes(1 To 10)
    
    For idx = 1 To 10
    
        Set TextBoxes(idx) = New CTextBoxes
        
        Set TextBoxes(idx).TBox = Me.Controls("TextBox" & idx)
        
        Me.Controls("TextBox" & idx).Tag = "ComboBox" & idx
    Next idx
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

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