【问题标题】:Index and match with an additional condition (excel only)索引并与附加条件匹配(仅限 Excel)
【发布时间】:2018-01-07 18:53:06
【问题描述】:

继续this 话题,我想再添加一个条件。我有以下startendaction 列:

11/9/2007   10/10/2008  SELL
11/14/2008  5/29/2009   BUY
11/27/2009  2/5/2010    SELL
10/8/2010   4/8/2011    SELL

从 2007 年 4 月 1 日到今天,我还有 target 天(周末除外)。我想做以下事情:

  • 如果target 日期在startend 日期范围内 AND actionSELL,然后打印出-1
  • 如果target 日期在startend 日期范围内 AND actionBUY,然后打印出1
  • 如果target日期不在startend日期范围内,则打印出0

一如既往地感谢任何指导/提示。

【问题讨论】:

    标签: excel date conditional


    【解决方案1】:

    我认为这段代码可以帮助你解决问题

    Dim dStart As Date
    Dim dEnd As Date
    Dim dDate As Date
    Dim iCol As Integer
    
    iCol = 2
    
    Do While Cells(iCol, 2).Value <> ""
        dStart = Format(Cells(iCol, 2).Value, "mm/dd/yyyy")
        dEnd = Format(Cells(iCol, 3).Value, "mm/dd/yyyy")
        if dDate > dStart and dDate < dEnd then
            if Cell(iCol,4).Value = "SELL" then
                printout -1
            else
                printout 1
            end if
        else
            printout 0
        end if
        iCol = iCol + 1
    Loop
    

    如果你只需要excel函数

    =IF(AND(J3 >= F3,J3 <= G3, H3="SELL"),-1,IF(AND(J3 >= F3,J3 <= G3, H3="BUY"),1,0))
    

    【讨论】:

      【解决方案2】:

      按照我对您之前引用的问题的回答的风格继续...

      index 列添加到包含值 1、2、3、... 的输入表中 =SUMPRODUCT((target&gt;=start)*(target&lt;=end)*index) 将指示target 属于输入表的哪一行(如果有)。如果表达式返回值 0,则 target 不属于任何日期范围。该表达式依赖于不重叠的日期范围(因此 target 最多可以在 1 个日期范围内)。如果日期范围可以重叠,那么如果 target 落在 2 个或更多日期范围内,则表达式将返回它所在范围的索引总和。这将是一个问题,因为例如,您不会能够判断结果为 3 是否意味着 target 属于第三个日期范围或前两个日期范围。

      将此表达式的结果用作INDEX() 函数的第二个参数,其中第一个参数是action 范围。然后,这将根据target 日期是否在买入范围内、卖出范围内或不在任何范围内来提供买入、卖出或#N/A。使用您想要的任何方法将这些结果转换为您想要的 +1、-1 和 0。

      【讨论】:

        猜你喜欢
        • 2021-09-20
        • 1970-01-01
        • 2018-10-07
        • 2019-10-22
        • 2019-12-28
        • 2023-04-01
        • 1970-01-01
        • 2021-01-08
        • 2022-12-18
        相关资源
        最近更新 更多