如何根据组合框(VBA)中的用户选择填写文本框 [英] How to fill in a textbox based on user selection in a Combobox (VBA)
问题描述
根据组合框的选择,我无法在用户窗体中填写文本框.我也尝试过使用.Caption,.Value和.Text.我在下面附上我想做的事的例子.同样,我也不是完全理解为什么当用户名是"userform"时,组合框为什么必须是Me.combobox的原因.如果您需要任何进一步的说明,请提出问题,尝试帮助您.
I am having troubles filling in a textbox in my userform based on selections of a combobox. Also i Have tried used .Caption, .Value, and .Text. i have attached an example below of what i am trying to do. Also i am not entirely understanding why the combobox has to be Me.combobox when the name is of the userform is "userform". if more clarifications is needed for anything just ask and ill try and help you out.
Private Sub UserForm_Initialize()
'combobox for column thickness
Me.ComboBox1.AddItem "W6 x 15"
Me.ComboBox1.AddItem "W8 x 2"
Me.ComboBox1.AddItem "W10 x 30 "
'trying to fill in textbox based on combobox
If Me.ComboBox1.Text = "W6 x 15" Then
UserForm1.TextBox17.Text = "W6 x 15"
ElseIf Me.Combobox1.Text = "W8 x 24" Then
Userform1.textbox17.Text = "W8 x 24"
End If
推荐答案
此处的问题是您只是在初始化组合框.组合框的值更改时,会有一个事件.那是当您必须更新文本框的文本时,请尝试以下操作:
The problem here is that you are just initializing the combobox. The combobox has an event, when its value changes. That's when you have to update the text of the textbox, so try this:
Private Sub UserForm_Initialize()
Me.ComboBox1.AddItem "W6 x 15"
Me.ComboBox1.AddItem "W8 x 2"
Me.ComboBox1.AddItem "W10 x 30"
End Sub
哦,同样,单词"Me"引用父"对象,在这种情况下是用户表单.如果要使用"UserForm1"代替"Me",那么它也可以使用.
Oh, also, the word "Me" references the "parent" object, which in this case is the userform. If you were to use "UserForm1" instead of "Me", it would also work.
Private Sub ComboBox1_Change()
Me.TextBox1.Text = Me.ComboBox1.Value
End Sub
这篇关于如何根据组合框(VBA)中的用户选择填写文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!