简体   繁体   English

如何根据组合框选择在文本框上显示文本

[英]How to Display Text on Textbox depending on the Combobox Selection

I am very new to VBA.我对 VBA 很陌生。 I have two columns:我有两列:

Column 1 abc第 1 栏 abc

Column 2 1 2 3第 2 列 1 2 3

So in if I select a from the combo box - I wanted to text box to show 1.因此,如果我从组合框中选择一个 - 我想在文本框中显示 1。

I have been trying to figure it out through the other posts here but could not get it to work.我一直试图通过这里的其他帖子弄清楚,但无法让它发挥作用。

If you could explain it to me that would be great!如果你能向我解释,那就太好了!

  Private Sub UserForm_Initialize()
 With Worksheets("Sheet1")
        ComboBox1.List = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value
    End With

End Sub

Thank you.谢谢你。

One way is to use VLOOKUP on the combobox value and put this code in the combo box change event so that it runs whenever it is changed.一种方法是在组合框值上使用 VLOOKUP 并将此代码放在组合框更改事件中,以便它在更改时运行。 Or you could assign it to a button.或者你可以将它分配给一个按钮。

Amend control names as necessary.根据需要修改控件名称。

Private Sub ComboBox1_Change()
    Me.TextBox1.Value = Application.VLookup(Me.ComboBox1.Value, Worksheets("Sheet1").Range("A1").CurrentRegion, 2, 0)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM