索引并匹配附加条件(仅 excel)
Index and match with an additional condition (excel only)
继续话题,我想再补充一个条件。我有以下 start
、end
和 action
列:
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
日期在 start
和 end
日期范围内
AND action
是SELL
,然后打印出-1
;
- 如果
target
日期在 start
和 end
日期范围内
ANDaction
是BUY
,然后打印出1
;
- 如果
target
天不在 start
和 end
日期范围内,则打印出 0
;
任何 guidance/tips 一如既往的赞赏。
我认为这段代码可以帮助您解决问题
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))
继续我对您引用的先前问题的回答方式...
将 index
列添加到包含值 1、2、3...的输入 table 表达式
=SUMPRODUCT((target>=start)*(target<=end)*index)
将指示 target
属于您的输入 table(如果有)的哪一行。如果表达式 return 的值为 0,则 target
不在任何日期范围内。该表达式依赖于不重叠的日期范围(因此 target
最多可以落在 1 个日期范围内)。如果日期范围可以重叠,那么如果 target
落在 2 个或更多日期范围内,则表达式将 return 它落入的范围的索引总和。这将是一个问题,因为,例如,您将无法判断结果 3 是否意味着 target
属于第三个日期范围或前两个日期范围。
使用此表达式的结果作为 INDEX()
函数的第二个参数,其中第一个参数是 action
范围。然后,根据 target
日期是否在买入范围内、卖出范围内或不在任何范围内,这将提供买入、卖出或 #N/A。使用您想要的任何方法将这些结果转换为您想要的 +1、-1 和 0。
继续start
、end
和 action
列:
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
日期在start
和end
日期范围内 ANDaction
是SELL
,然后打印出-1
; - 如果
target
日期在start
和end
日期范围内 ANDaction
是BUY
,然后打印出1
; - 如果
target
天不在start
和end
日期范围内,则打印出0
;
任何 guidance/tips 一如既往的赞赏。
我认为这段代码可以帮助您解决问题
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))
继续我对您引用的先前问题的回答方式...
将 index
列添加到包含值 1、2、3...的输入 table 表达式
=SUMPRODUCT((target>=start)*(target<=end)*index)
将指示 target
属于您的输入 table(如果有)的哪一行。如果表达式 return 的值为 0,则 target
不在任何日期范围内。该表达式依赖于不重叠的日期范围(因此 target
最多可以落在 1 个日期范围内)。如果日期范围可以重叠,那么如果 target
落在 2 个或更多日期范围内,则表达式将 return 它落入的范围的索引总和。这将是一个问题,因为,例如,您将无法判断结果 3 是否意味着 target
属于第三个日期范围或前两个日期范围。
使用此表达式的结果作为 INDEX()
函数的第二个参数,其中第一个参数是 action
范围。然后,根据 target
日期是否在买入范围内、卖出范围内或不在任何范围内,这将提供买入、卖出或 #N/A。使用您想要的任何方法将这些结果转换为您想要的 +1、-1 和 0。