动态 select 使用月份和年份的列,然后根据另一列中的条件对值求和

Dynamically select a column using month and year, then sumif the values based on criteria in another column

这是我要实现的目标的图片:

我不精通创建自己的 VBA 脚本,但如果有时间剖析它,我可以理解别人编写的代码。如果这个objective不用VBA也能解决,那是我的偏好。我已经使用 INDEXMATCH 进行了探索,但一无所获。有什么建议么?

我使用的是 Excel 2013。

找到正确列的示例,如我的评论中所述(未经测试的代码):

arr = array("Jan","Feb") 'you can do the rest
x = 2018 'first year in question, to make the math easier
s = 4 'column your first month is in
y = cells(1,2).value - x
z = application.match(left(cells(1,1).value,3),arr,0)
'all that gets you to:
c = s+(y*12)+z

然后您可以使用最后的列号 c 来执行您的 Application.Sumifs(),其中您指定 actual/plan 作为您的条件之一。

我会"cheat"

我的月份列表在 AI1:AI12 中,年份列表在 AJ1:AJ2 中(分别用于 A1 和 B1 中的下拉菜单)。将它们转移到其他地方更多年。

然后在A2:

=MATCH(A1,AI1:AI12,0)

B2中:

=IF(MATCH(B1,AJ1:AJ2,0)=1,0,12 *( MATCH(B1,AJ1:AJ2,0)-1))

D1O 中拖到 AA10:

=SUMIF($C:$C,"Plan",D3:D8)

注意:您可以删除硬编码 "Plan" 并改为引用单元格。

结果 C1

=OFFSET(D10,,B2+A2-1,1,1)

您基本上是在使用一个公式来确定从第 1 年的第 1 个月偏移多远,以获得第 10 行中的当前月份和年份条件总和。