从左到右对多列中的行进行计数,直到满足特定条件为止

时间:2019-06-03 08:57:11

标签: excel vba if-statement count

我有下表。我将根据其他无关信息引用一个特定的数字。可以说具体的数字是30。我首先需要从我的9月列表中向下计数30个数字,然后移至10月再到11月,直到计数达到30。然后我需要对所有缺失值进行计数,直到下一个值达到从上一个任务。因此,对于本示例,第30个数字将是11月19日。失踪人数应为11月15日的55岁(如果我算是正确的话)。然后,该值将存储在单元格中。

我使用以下公式获得了错过的日子:=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月有数据,我也想从4月开始。对此的另一种思考方式是3月是在2019年,4月是在2018年。那么,我如何才能使代码从12月跳回到1月呢?假设Z列是12月,A列是1月。我并不一定希望我的代码仅从左到右阅读。如果年份更改,则需要先参考其他单元格,然后再跳回起点。

我为冗长而道歉,但这是我尽力而为的解释。让我知道您是否有任何疑问,或者是否可以向任何人提供更多示例,图片等。

1 个答案:

答案 0 :(得分:0)

我认为您应该将数据表重新组织为如下形式:

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

之后,您可以轻松地管理计数,通过过滤查找所需的内容,指定开始日期等

相关问题