如果 Vlookup 找到与插入的数字相关的信息,则 Userform 文本框将填充
Userform textbox fills in if Vlookup finds the information related to the numbers inserted
我有一个用户表单,人们必须在其中填写数据。如果数据已经存在,当他们将信息放入DocumentTitleBox时,其他文本框应该会自动填充。
我的代码适用于字母,但不适用于数字。
例如,当我输入 "aaa" 时,它会 returns vlookup 值。但是如果我输入“123”,它不会做任何事情,即使它有一个 vlookup。
我不明白为什么。这是我的代码的一部分:
Private Sub DocumentTitleBox_Change()
On Error Resume Next
Result = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 2, False)
FIND = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 1, False)
On Error GoTo 0
If FIND = DocumentTitleBox.Value Then
NameBox.Value = Result
End If
提前致谢!
如果 DocumentTitleBox
是文本框,请尝试使用 DocumentTitleBox.Text
而不是 DocumentTitleBox.Value
。
我一直用这种东西。可以清理和东西,但我喜欢灵活性,我一直在改变东西,所以这对我有用。
Private Sub DocumentTitleBox_Change()
If IsNumeric(DocumentTitleBox.Value) Then
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value + 0, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
Else
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
End If
If Not Find Like "Not Present" Then
NameBox.Value = Find
Else
NameBox.Value = ""
End If
End Sub
PS:我不知道如何使用 strings/numbers 来避免匹配函数的奇怪行为,所以我只使用 +0 和 IsNumeric 技巧。需要注意的一件事是区分大小写,根据需要进行调整,现在不是。
我有一个用户表单,人们必须在其中填写数据。如果数据已经存在,当他们将信息放入DocumentTitleBox时,其他文本框应该会自动填充。
我的代码适用于字母,但不适用于数字。
例如,当我输入 "aaa" 时,它会 returns vlookup 值。但是如果我输入“123”,它不会做任何事情,即使它有一个 vlookup。
我不明白为什么。这是我的代码的一部分:
Private Sub DocumentTitleBox_Change()
On Error Resume Next
Result = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 2, False)
FIND = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 1, False)
On Error GoTo 0
If FIND = DocumentTitleBox.Value Then
NameBox.Value = Result
End If
提前致谢!
如果 DocumentTitleBox
是文本框,请尝试使用 DocumentTitleBox.Text
而不是 DocumentTitleBox.Value
。
我一直用这种东西。可以清理和东西,但我喜欢灵活性,我一直在改变东西,所以这对我有用。
Private Sub DocumentTitleBox_Change()
If IsNumeric(DocumentTitleBox.Value) Then
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value + 0, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
Else
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
End If
If Not Find Like "Not Present" Then
NameBox.Value = Find
Else
NameBox.Value = ""
End If
End Sub
PS:我不知道如何使用 strings/numbers 来避免匹配函数的奇怪行为,所以我只使用 +0 和 IsNumeric 技巧。需要注意的一件事是区分大小写,根据需要进行调整,现在不是。