简体   繁体   English

使用 VLOOKUP()

[英]Using VLOOKUP()

I am using Excel's VLOOKUP() first time and I am having hard time with it.我第一次使用 Excel 的 VLOOKUP() 并且我很难使用它。 Here is the screenshot:这是屏幕截图:

在此处输入图片说明

I wrote a formula to cell B8;我在单元格 B8 中写了一个公式; =VLOOKUP(C8, $A$2:$C$5, IF(C8 = "W", 2,3))

I was expecting as a result 1, but I am getting the wrong result 0.25.我期待的结果是 1,但我得到了错误的结果 0.25。 Am I doing something wrong?难道我做错了什么?

Thanks for the help!谢谢您的帮助!

A vlookup looks for a value in the leftmost column of a table and then returns a value from the row that the searched for value is found (how far along that row is determined by you). vlookup 在表的最左边的列中查找值,然后从找到搜索值的行返回一个值(该行的距离由您决定)。

In your example situation, you need to add in the 4th part below (NearOrExact)在您的示例情况下,您需要添加下面的第 4 部分(NearOrExact)

The vlookup formula consists of 4 parts: vlookup 公式由 4 部分组成:

1. SearchFor this is the value that is being searched for. 

2. WhereToSearch this is the range in which to search and in which the answer lives. 

3. WhichColumn this is the column in the WhereToSearch range which has the answer in it. 

4. NearOrExact you decide whether the vlookup should search for a close value or an exact value.

Structure of the vlookup formula: =vlookup ( SearchFor , WhereToSearch , WhichColumn , NearOrExact ) vlookup 公式的结构:=vlookup ( SearchFor , WhereToSearch , whichColumn , NearOrExact )

From < http://www.excelvlookuphelp.com/how-to-use-vlookup/ >来自 < http://www.excelvlookuphelp.com/how-to-use-vlookup/ >

you were very close, vlookup takes teh lookup value first, so change the first C8 to A8你已经很接近了,vlookup首先获取查找值,因此将第一个C8更改为A8

EDIT: As Barry Houdini points out in comments, also add FALSE as the fourth parameter编辑:正如 Barry Houdini 在评论中指出的那样,还添加 FALSE 作为第四个参数

Sub IsoscelesTriangle1_Click() Dim finder As Worksheet, dataws As Worksheet Dim goalslastrow As Long, datalastrow As Long, x As Long Dim datarng As Range Sub IsoscelesTriangle1_Click() Dim finder As Worksheet, dataws As Worksheet Dim goallastrow As Long, datalastrow As Long, x As Long Dim datarng As Range

Set finder = ThisWorkbook.Worksheets("Sheet2") Set dataws = ThisWorkbook.Worksheets("Sheet1") Set finder = ThisWorkbook.Worksheets("Sheet2") Set dataws = ThisWorkbook.Worksheets("Sheet1")

goalslastrow = finder.Range("A" & Rows.Count).End(xlUp).Row datalastrow = dataws.Range("A" & Rows.Count).End(xlUp).Row goallastrow = finder.Range("A" & Rows.Count).End(xlUp).Row datalastrow = dataws.Range("A" & Rows.Count).End(xlUp).Row

Set datarng = dataws.Range("A2:C" & datalastrow)设置 datarng = dataws.Range("A2:C" & datalastrow)

For x = 2 To goalslastrow On Error Resume Next finder.Range("C" & x).Value = Application.WorksheetFunction.VLookup( _ finder.Range("A" & x).Value, datarng, 2, False)对于 x = 2 错误恢复下一个 finder.Range("C" & x).Value = Application.WorksheetFunction.VLookup( _ finder.Range("A" & x).Value, datarng, 2, False)

Next x下一个

For x = 2 To goalslastrow On Error Resume Next finder.Range("i" & x).Value = Application.WorksheetFunction.VLookup( _ finder.Range("A" & x).Value, datarng, 3, False)对于 x = 2 错误恢复下一个 finder.Range("i" & x).Value = Application.WorksheetFunction.VLookup( _ finder.Range("A" & x).Value, datarng, 3, False)

Next x下一个

End Sub结束子

for vba对于 vba

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

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