【发布时间】:2019-06-03 18:27:18
【问题描述】:
我有下表。我将根据其他无关信息引用一个特定的数字。假设具体数字是 30。我首先需要在 9 月的列表中数 30 个数字,然后移到 10 月,然后是 11 月,直到计数达到 30。然后我需要计算所有缺失值,直到下一个值达到第 30 个上一个任务。所以对于这个例子,第 30 个数字是 11 月 19 日。失踪人数应该是 55 人,11 月 15 日(如果我数对了的话)。然后将该值存储在一个单元格中。
我通过以下公式获得错过的天数:=IFERROR(SMALL(IF(ISERROR(MATCH(ROW(L$1:INDEX(L:L,N$2)),M$2:INDEX(M:M,COUNT(M:M)+ROW(M$1)),0)),ROW(L$1:INDEX(L:L,N$2))),ROW()-ROW(L$1)),"")(列参考见表2)
如果月份列中没有数据,则最大列值将为空,因此错过的列也将没有数据。我使用以下公式进行设置:
=IF(COUNTA(M:M)>1,31,"")(列参考见表2)
Table 1
September max missed October max missed November max missed
1 30 4 1 31 2 2 30 1
2 6 3 6 7 3
3 7 4 7 9 4
5 11 5 8 10 5
8 12 12 9 11 6
9 13 15 10 16 8
10 14 20 11 17 12
15 16 28 13 18 13
22 17 30 14 19 14
23 18 31 16 20 15
24 19 17 22 21
25 20 18 27 23
29 21 19 28 24
26 21 25
27 22 26
28 23 29
30 24 30
25
26
27
29
Table 2
L M N O
(blank) September max missed
我知道如何写这个,但不知道语法:
x = Select(Range("G8").Value)
'value that holds specific value (30 for above example)
If x < 31 Then
'30 days in September
y = Count(M2:M32) Until = x
'values in September
z = Count(O2:O32) Until = value of y - 1
'What if the last value is the 30th of September, how would you stop on August 31st?
Range("A1").Value = z
'value of z is stored in cell A1
Elseif x < 62 Then
'61 days in September and October
y2 = Count(M2:M32) & Count(Q2:Q32) Until = x
'Values in September and October
z2 = Count(R2:R32) & (S2:S32) Until =value of -1
'Again, if the last value is the 31st of October how would you stop on September 30th?
Range("A1").Value = z
'Value of z is stored in cell A1
Elseif
'continue for each month (12 times)
End If
有几件事可能会导致我的建议出现一些问题(我刚刚想到的)。我将如何决定我的开始月份?假设我想引用一个特定的单元格并且该单元格包含数字 4。所以我想从四月开始,即使我在三月有数据。另一种思考方式是 3 月是 2019 年,4 月是 2018 年。那么我怎样才能让代码从 12 月跳回 1 月呢?假设 Z 列是 12 月,A 列是 1 月。我不一定希望我的代码只能从左到右阅读。它需要参考另一个单元格开始,然后如果年份发生变化,则跳回起点。
对于冗长的内容我深表歉意,但这是我尽力解释的。如果您有任何问题,或者我可以为任何人提供更多示例、图片等,请告诉我。
【问题讨论】:
-
你想在所有月份都做这个逻辑吗?
标签: excel vba if-statement count