简体   繁体   English

如何在输入要查找的值的同一单元格中返回vlookup的值?

[英]How to return the value of vlookup in same cell in which the value to be looked up is entered?

I want to return the result of vlookup in the same cell in which the value to be looked up is entered . 我想在输入要查找的值的同一单元格中返回vlookup的结果。 Figure 1 shows the value which is to be looked up 图1显示了要查找的值

Figure 1 图1

在此处输入图片说明

I want the value in A1 to change to vlookup value of t001 when I press enter as shown in figure 2 我希望在按Enter时将A1中的值更改为t001的vlookup值,如图2所示

Figure 2 图2

在此处输入图片说明

the lookup table is present in sheet2 t001 trew t002 rty 在sheet2中存在查找表t001 trew t002 rty

Following code I have written but it is not working; 按照我编写的代码,但无法正常工作; kindly help me in this regard 请在这方面帮助我

Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A1:A114")) Is Nothing Then
    If WorksheetFunction.IsNumber(Target.Value) Then
        Application.EnableEvents = False
        Table2 = Sheet2.Range("C2:D3")
        Target.Value = Application.WorksheetFunction.VLookup(Target.Value, Table2, 2, False)
        Application.EnableEvents = True
    End If
End If
End Sub

By using 通过使用

If WorksheetFunction.IsNumber(Target.Value) Then
...
End If

You are making sure that the interior of the 'if' clause will not be executed if the target value is not a number. 如果目标值不是数字,请确保不会执行'if'子句的内部。 For example having Table2 例如具有Table2

abc ¦ not working
-----------------
123 ¦  working

after typing 'abc' nothing happens and after typing 123 the number disappears and 'working' appears 键入“ abc”后没有任何反应,键入123后数字消失并且出现“正在工作”

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

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