【问题标题】:Count rows in multiple columns left to right until specific criteria is met从左到右计算多列中的行数,直到满足特定条件
【发布时间】: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


【解决方案1】:

我认为你应该将你的数据表重新组织成这样:

Day        Status
01.09.2018 ok
02.09.2018 ok
03.09.2018 ok
04.09.2018 missed
05.09.2018 ok
06.09.2018 missed
07.09.2018 missed
08.09.2018 ok
09.09.2018 ok
10.09.2018 ok
11.09.2018 missed
12.09.2018 missed
13.09.2018 missed
14.09.2018 missed
15.09.2018 ok
16.09.2018 missed
17.09.2018 missed
18.09.2018 missed
19.09.2018 missed
20.09.2018 missed
21.09.2018 missed
22.09.2018 ok
23.09.2018 ok
24.09.2018 ok
25.09.2018 ok
26.09.2018 missed
27.09.2018 missed
28.09.2018 missed
29.09.2018 ok
30.09.2018 missed
01.10.2018 ok
02.10.2018 ok
03.10.2018 ok
04.10.2018 ok
05.10.2018 ok
06.10.2018 ok
07.10.2018 ok
08.10.2018 ok
09.10.2018 ok
10.10.2018 ok
11.10.2018 ok
12.10.2018 ok
13.10.2018 ok
14.10.2018 ok
15.10.2018 ok
16.10.2018 ok
17.10.2018 ok
18.10.2018 ok
19.10.2018 ok
20.10.2018 ok
21.10.2018 ok
22.10.2018 ok
23.10.2018 ok
24.10.2018 ok
25.10.2018 ok
26.10.2018 ok
27.10.2018 ok
28.10.2018 ok
29.10.2018 ok
30.10.2018 ok
31.10.2018 missed

之后,您可以轻松管理您的计数,通过过滤、指定日期开始等找到您想要的任何内容

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2016-05-09
    • 2016-04-29
    • 2011-07-10
    相关资源
    最近更新 更多