不使用 application.vlookup 的用户表单 vlookup
userform vlookup without using application.vlookup
我目前正在尝试创建一个用户窗体,它将调用 B、C、D 列与 A 列值的匹配值。为了将 B、C 和 D 列的值显示到 textbox2、textbox3 和 textbox4 中,我必须将 A 列的值放入 textbox1。我的 sheet 中的数据是不确定的,所以我尽可能不想使用 application.vlookup
。我为此使用了同事提供给我的代码,该代码实际上正在处理我创建的一些用户表单。但是,我现在用的时候就是不行。
Private Sub Textbox1_AfterUpdate()
If Textbox1.Value = "" Then
Textbox2 = ""
Textbox3 = ""
Textbox4 = ""
Exit Sub
Else
x = 2
Do Until Sheet2.Cells(x, "A") = Textbox1.Value
If Sheet2.Cells(x, "C").Value = "" Then
Textbox2 = ""
Textbox3 = ""
Textbox4 = ""
Exit Sub
End If
x = x + 1
Loop
Textbox2 = Sheet2.Cells(x, "B")
Textbox3 = Sheet2.Cells(x, "C")
Textbox4 = Sheet2.Cells(x, "D")
End If
End Sub
希望您能研究一下,指出错误并提出更正建议。
谢谢!
您对 VLookup
的异议无效。不需要为我的查找列创建名称,也不需要重做名称范围作为查找值
只需对整列进行搜索。
也就是说,Application.Match
在这种情况下实际上更有用
您的代码,已重构
Private Sub Textbox1_AfterUpdate()
Dim x As Variant
If Textbox1.Value <> vbNullString Then
With Sheet2
x = Application.Match(Textbox1.Value, .Columns(1), 0)
If Not IsError(x) Then
If .Cells(x, "C").Value <> vbNullString and x >= 2 Then
Textbox2 = .Cells(x, "B")
Textbox3 = .Cells(x, "C")
Textbox4 = .Cells(x, "D")
Exit Sub
End If
End If
End With
End If
Textbox2 = vbNullString
Textbox3 = vbNullString
Textbox4 = vbNullString
End Sub
在向 TextBox1 添加数据之前
向 TextBox1 添加数据后
也许您可以坚持使用之前使用 vlookup
的代码,只需稍微更改该命名范围并添加另一个命名范围作为左上角数据的标记。
假设你有这个 table(故意从 A1 偏移):
命名范围 TopLeftData
设置为 B3
。然后将名称 Names
设置为此公式 =OFFSET(TopLeftData,1,0,COUNTA(Sheet1!B:B)-1,2)
(您需要更改 "Sheet1" 或有另一个名称指向该列),然后 vlookup使用 Names 可以正常工作。
只要有效数据低于 TopLeftData 并且没有数据高于它,名称 Names 将动态工作。
试试这个:
Private Sub Textbox1_AfterUpdate()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("sheet1")
toFind = TextBox1.Value
Set trouve = ws.Columns("A").Find(what:=toFind, LookIn:=xlValues, Lookat:=xlWhole)
If trouve Is Nothing Then
Debug.Print "Not Found ! "
Else
firstAddress = trouve.Address
foundRow = trouve.Row
TextBox2.Value = ws.Cells(foundRow, 2)
TextBox3.Value = ws.Cells(foundRow, 3)
TextBox4.Value = ws.Cells(foundRow, 4)
End If
End Sub
我目前正在尝试创建一个用户窗体,它将调用 B、C、D 列与 A 列值的匹配值。为了将 B、C 和 D 列的值显示到 textbox2、textbox3 和 textbox4 中,我必须将 A 列的值放入 textbox1。我的 sheet 中的数据是不确定的,所以我尽可能不想使用 application.vlookup
。我为此使用了同事提供给我的代码,该代码实际上正在处理我创建的一些用户表单。但是,我现在用的时候就是不行。
Private Sub Textbox1_AfterUpdate()
If Textbox1.Value = "" Then
Textbox2 = ""
Textbox3 = ""
Textbox4 = ""
Exit Sub
Else
x = 2
Do Until Sheet2.Cells(x, "A") = Textbox1.Value
If Sheet2.Cells(x, "C").Value = "" Then
Textbox2 = ""
Textbox3 = ""
Textbox4 = ""
Exit Sub
End If
x = x + 1
Loop
Textbox2 = Sheet2.Cells(x, "B")
Textbox3 = Sheet2.Cells(x, "C")
Textbox4 = Sheet2.Cells(x, "D")
End If
End Sub
希望您能研究一下,指出错误并提出更正建议。
谢谢!
您对 VLookup
的异议无效。不需要为我的查找列创建名称,也不需要重做名称范围作为查找值
只需对整列进行搜索。
也就是说,Application.Match
在这种情况下实际上更有用
您的代码,已重构
Private Sub Textbox1_AfterUpdate()
Dim x As Variant
If Textbox1.Value <> vbNullString Then
With Sheet2
x = Application.Match(Textbox1.Value, .Columns(1), 0)
If Not IsError(x) Then
If .Cells(x, "C").Value <> vbNullString and x >= 2 Then
Textbox2 = .Cells(x, "B")
Textbox3 = .Cells(x, "C")
Textbox4 = .Cells(x, "D")
Exit Sub
End If
End If
End With
End If
Textbox2 = vbNullString
Textbox3 = vbNullString
Textbox4 = vbNullString
End Sub
在向 TextBox1 添加数据之前
向 TextBox1 添加数据后
也许您可以坚持使用之前使用 vlookup
的代码,只需稍微更改该命名范围并添加另一个命名范围作为左上角数据的标记。
假设你有这个 table(故意从 A1 偏移):
命名范围 TopLeftData
设置为 B3
。然后将名称 Names
设置为此公式 =OFFSET(TopLeftData,1,0,COUNTA(Sheet1!B:B)-1,2)
(您需要更改 "Sheet1" 或有另一个名称指向该列),然后 vlookup使用 Names 可以正常工作。
只要有效数据低于 TopLeftData 并且没有数据高于它,名称 Names 将动态工作。
试试这个:
Private Sub Textbox1_AfterUpdate()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("sheet1")
toFind = TextBox1.Value
Set trouve = ws.Columns("A").Find(what:=toFind, LookIn:=xlValues, Lookat:=xlWhole)
If trouve Is Nothing Then
Debug.Print "Not Found ! "
Else
firstAddress = trouve.Address
foundRow = trouve.Row
TextBox2.Value = ws.Cells(foundRow, 2)
TextBox3.Value = ws.Cells(foundRow, 3)
TextBox4.Value = ws.Cells(foundRow, 4)
End If
End Sub