VLookUp ComboBox 输入以更新 TextBox 值
VLookUp ComboBox input to update TextBox values
我的问题是在我的用户窗体中从下拉列表 (ComboBox) 中选择一个国家后,我的文本框没有显示任何输出。什么都没发生。文本框应该对 ComboBox 中选择的任何值执行 VLookUp。 ComboBox(名称)是 "Country".
其中一个文本框的代码:
Private Sub TextBox2_Change()
Dim myRange As Range
Set myRange = Worksheets("All Countries Validation").Range("A:R")
TextBox2.Value = Application.WorksheetFunction.VLookup(Country.Value, myRange, 2, False)
您必须使用 Country_Change()
事件处理程序而不是 TextBox2_Change()
一个
Option Explicit
Private Sub Country_Change()
Dim myRange As Range, f As Range
Set myRange = Worksheets("All Countries Validation").Range("A:A")
Set f = myRange.Find(What:=Country.Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) '<--| try and find combobox selected value
If f Is Nothing Then '<--| if not found ...
TextBox2.Value = "" '<--| ... then clear textbox
Else'<--| ... otherwise...
TextBox2.Value = f.Offset(, 1) '<--| ... fill it with proper value
End If
End Sub
我的问题是在我的用户窗体中从下拉列表 (ComboBox) 中选择一个国家后,我的文本框没有显示任何输出。什么都没发生。文本框应该对 ComboBox 中选择的任何值执行 VLookUp。 ComboBox(名称)是 "Country".
其中一个文本框的代码:
Private Sub TextBox2_Change()
Dim myRange As Range
Set myRange = Worksheets("All Countries Validation").Range("A:R")
TextBox2.Value = Application.WorksheetFunction.VLookup(Country.Value, myRange, 2, False)
您必须使用 Country_Change()
事件处理程序而不是 TextBox2_Change()
一个
Option Explicit
Private Sub Country_Change()
Dim myRange As Range, f As Range
Set myRange = Worksheets("All Countries Validation").Range("A:A")
Set f = myRange.Find(What:=Country.Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) '<--| try and find combobox selected value
If f Is Nothing Then '<--| if not found ...
TextBox2.Value = "" '<--| ... then clear textbox
Else'<--| ... otherwise...
TextBox2.Value = f.Offset(, 1) '<--| ... fill it with proper value
End If
End Sub