简体   繁体   English

如何使用 2 个条件在 excel 中进行 Vlookup

[英]How to Vlookup in excel with 2 conditions

I'm evaluating my portfolio, and I make a record of it in some form like this:我正在评估我的投资组合,并以如下形式记录下来:

擅长

I wish to get the profit% of those stocks, from either;我希望从任何一个中获得这些股票的利润百分比;

  1. Stocks with sold: (Price at SELL/Price at BUY)-1已售出的股票:(卖出价/买入价)-1

  2. Stocks not sold: (Current Price/Price at BUY)-1未售出的股票:(当前价格/买入价)-1

For 1), I was thinking of getting the Last Price (Col E) of Stock A by using VLOOKUP conditioning with 'Stock A' of Col B and a 'SELL' of Col C (then obviously using the VLOOKUP again with 'SELL' condition changed to 'BUY').对于 1),我正在考虑通过使用 VLOOKUP 条件和 Col B 的“股票 A”和 Col C 的“卖出”来获得股票 A 的最后价格(Col E)(然后显然再次使用 VLOOKUP 和“SELL”条件更改为“买入”)。

Is there a way to look up a value with two conditions?有没有办法在两个条件下查找一个值? Any other suggestions (using a touch of VBA for eg) would be greatly appreciated.任何其他建议(例如,使用触摸 VBA)将不胜感激。

A VLOOKUP is, in effect, a special form of INDEX MATCH (or XLOOKUP , from Office365 onwards). VLOOKUP实际上是INDEX MATCH的一种特殊形式(或XLOOKUP ,从 Office365 开始)。 In other words, VLOOKUP("Value", A:C, 3, FALSE) is the same as INDEX(C:C, MATCH("Value", A:A, 0)) , and VLOOKUP("Value", A:C, 3, TRUE) is the same as INDEX(C:C, MATCH("Value", A:A, 1))换句话说, VLOOKUP("Value", A:C, 3, FALSE)INDEX(C:C, MATCH("Value", A:A, 0))VLOOKUP("Value", A:C, 3, TRUE)INDEX(C:C, MATCH("Value", A:A, 1))相同

Then, to create a VLOOKUP equivalent that checks multiple columns, we need to create a MATCH function to do the same.然后,要创建一个检查多列的VLOOKUP等效项,我们需要创建一个MATCH function 来执行相同的操作。 Fortunately, this is possibly using Array Calculations.幸运的是,这可能是使用数组计算。

Essentially, we replace the second argument of the MATCH (the list of values to look for) with a formula which creates an Array based on our Conditions, and we Search the list for where the conditions are True.本质上,我们将MATCH的第二个参数(要查找的值列表)替换为根据我们的条件创建数组的公式,然后我们在列表中搜索条件为真的位置。 For example, if our Conditions are Column B = "Stock A" and Column C = "BUY" , then our Array Calculation is (B:B="Stock A")*(C:C="Buy") , and our MATCH looks like this:例如,如果我们的条件是Column B = "Stock A"Column C = "BUY" ,那么我们的数组计算是(B:B="Stock A")*(C:C="Buy") ,我们的MATCH看起来像这样:

MATCH(1, (B:B="Stock A")*(C:C="BUY"), 0)

(At a Boolean or Bitwise level, * is the same as AND : TRUE*TRUE=TRUE , FALSE*FALSE=FALSE , TRUE*FALSE=FALSE ) (在 Boolean 或按位级别, *AND相同: TRUE*TRUE=TRUEFALSE*FALSE=FALSETRUE*FALSE=FALSE

Rather than using Entire Columns (which are Slow), it is often best to limit the number of rows.与其使用整列(很慢),通常最好限制行数。 You can either do this manually:您可以手动执行此操作:

=INDEX($E$1:$E$5, MATCH(1, ($B$1:$B$5="Stock A")*($C$1:$C$5="BUY"), 0))

Or you can do it dynamically with INDEX and COUNTA :或者您可以使用INDEXCOUNTA动态执行此操作:

=INDEX($E$1:INDEX($E:$E, COUNTA($A:$A)), MATCH(1, ($B$1:INDEX($B:$B, COUNTA($A:$A)), ="Stock A")*($C$1:INDEX($C:$C, COUNTA($A:$A)), ="BUY"), 0))

Final note: the MATCH function will start at the top of the list, and work down until the first match.最后注意: MATCH function 将从列表的顶部开始,一直向下直到第一个匹配项。 If these are in Date Order, and you want to find the last match, then Office356 users have the XMATCH function available, which has an extra argument used to decide if the search is First-to-Last ( XMATCH(.., .., .., 1) ), Last-to-First ( XMATCH(.., .., .., -1) ), Ascending Binary Search ( XMATCH(.., .., .., 2) ), or Descending Binary Search ( XMATCH(.., .., .., -2) )如果这些按日期顺序排列,并且您想找到最后一个匹配项,那么 Office356 用户可以使用XMATCH function ,它有一个额外的参数用于决定搜索是否XMATCH(.., .., .., 1) ),从后到先( XMATCH(.., .., .., -1) ),升序二进制搜索( XMATCH(.., .., .., 2) ),或降序二分查找 ( XMATCH(.., .., .., -2) )

Otherwise, you may have to use SUMPRODUCT to get the MAX date that matches your criteria, and include that in your MATCH conditions ( SUMPRODUCT(MAX(A:A*(B:B="Stock A")*(C:C="BUY")) )否则,您可能必须使用SUMPRODUCT来获取符合您条件的MAX日期,并将其包含在您的MATCH条件中( SUMPRODUCT(MAX(A:A*(B:B="Stock A")*(C:C="BUY"))

Is there a way to look up a value with two conditions?有没有办法在两个条件下查找一个值?

There is a very simple solution - you can concatenate (using & ) the two columns holding conditions for your search into one and then base your VLOOKUP search on that column.有一个非常简单的解决方案 - 您可以将保存搜索条件的两列连接(使用& )为一列,然后将VLOOKUP搜索基于该列。 Remember to put this concatenated column at beginning of your table.请记住将此连接列放在表格的开头。

If you can ensure that the combination Stock A - BUY and Stock A - SELL occur only once like in如果您可以确保Stock A - BUYStock A - SELL的组合仅发生一次,例如

在此处输入图像描述

and never twice like in从来没有像在

在此处输入图像描述

Then you can use the SUMIFS function to "lookup" the value.然后您可以使用SUMIFS function 来“查找”该值。 Because the sum of only one value will always be the value itself:因为只有一个值的总和将始终是值本身:

=SUMIFS(E:E,B:B,"Stock A",C:C,"BUY")/SUMIFS(E:E,B:B,"Stock A",C:C,"SELL") -1

To be sure there is only one entry per combination you can use the COUNTIFS to check it and return a warning in case of multiple entries (which would result in a false value):为确保每个组合只有一个条目,您可以使用COUNTIFS检查它并在多个条目的情况下返回警告(这将导致错误值):

=IF(AND(COUNTIFS(B:B,"Stock A",C:C,"BUY")=1,COUNTIFS(B:B,"Stock A",C:C,"SELL")=1), SUMIFS(E:E,B:B,"Stock A",C:C,"BUY")/SUMIFS(E:E,B:B,"Stock A",C:C,"SELL") -1,"multiple entries found")

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

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