【问题标题】:How to calculate "customer months" in Excel如何在 Excel 中计算“客户月数”
【发布时间】:2018-06-26 10:50:11
【问题描述】:

在任何给定日期计算客户月数的最简单方法是什么?

#   start   end
1   01/16   01/17
2   01/16   07/17
3   07/16   01/17

假设开始月份包括在内,结束月份不包括在内。

我想要的结果是:

<= (start of) 01/16: total customer months = 0
e.g. (start of) 03/16 = 4 (2 customers for each 2 months)
e.g. (start of) 08/16 = 15 (2 customers for each 7 months, 1 customer for 1 month)
>= (start of) 07/17 = 36 (12 + 18 + 6)

【问题讨论】:

  • array formula 是对数据集执行计算的最佳方式。
  • 这就是我的想法,因此标签。不过,我不知道如何添加必要的条件来完成这项工作。
  • 网上有很多教程和其他资源可以教你如何使用数组公式
  • “很荣幸成为这个丰富的知识共享社区的一员!”

标签: excel google-sheets array-formulas


【解决方案1】:
  1. 将您的数据放入 Excel 表中,其中Table1 将包含成员资格的开始和结束

  2. 创建以下函数以返回经过的客户成员资格计算

    Private Function get_duration(ByVal checkdate As Range) As Integer
    
    Dim tbl As ListObject: Set tbl = Sheets("Sheet1").ListObjects("Table1")
    Dim duration_check As Integer
    Dim total_duration As Long
    
    total_duration = 0
    
        For Each cell In tbl.ListColumns(1).DataBodyRange
            If (DateDiff("m", cell.Offset(0, 1), checkdate) <= 0) Then
                duration_check = DateDiff("m", cell, checkdate)
                If duration_check < 0 Then
                    duration_check = 0
                End If
    
                total_duration = total_duration + duration_check
            End If
        Next cell
    
    get_duration = total_duration
    
    End Function
    
  3. 为函数提供您要检查的特定单元格范围。并做了! 从技术上讲,这是已回答的问题,但请查看下面的部分以了解如何实现它


  1. 实践中的实施(真实示例)。创建Table2,它将包含您要检查的所有日期(我刚刚包含了您在问题示例中描述的内容)

  2. 创建一个简单的过程来遍历所有CheckDate调用我们的get_duration() 函数

    Private Sub loop_through_checkdates()
    
    Dim tbl2 As ListObject: Set tbl2 = Sheets("Sheet1").ListObjects("Table2")
    
    For Each cell In tbl2.ListColumns(1).DataBodyRange
        cell.Offset(0, 1) = get_duration(cell)
    Next cell
    
    End Sub
    
  3. 提供您问题中预期的结果

【讨论】:

  • 完美!谢谢你,克里斯。
  • 很高兴它有所帮助,但为了将来参考,您确实应该包含您当前工作的代码。我认为这也是为什么 ashlee 不愿发布任何具体解决方案的原因。我相信她的意思是好的。
猜你喜欢
  • 1970-01-01
  • 2013-11-17
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多