如何在 Excel 中计算 "customer months"
How to calculate "customer months" in Excel
计算在任何给定日期的客户月数的最简单方法是什么?
# 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)
将您的数据放入 Excel Table,其中 Table1
将包含成员资格的开始和结束时间
创建以下函数以return 已过客户会员计算
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
为函数提供您要检查的特定单元格范围。并做了! 从技术上讲,这是已回答的问题,但请查看下面的部分以了解如何实施
实践中的实现(实例)。创建 Table2
,其中将包含您要检查的所有日期(我只是包含了您在问题示例中描述的内容)
创建一个简单的过程来遍历所有 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
提供您问题中预期的结果
计算在任何给定日期的客户月数的最简单方法是什么?
# 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)
将您的数据放入 Excel Table,其中
Table1
将包含成员资格的开始和结束时间创建以下函数以return 已过客户会员计算
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
为函数提供您要检查的特定单元格范围。并做了! 从技术上讲,这是已回答的问题,但请查看下面的部分以了解如何实施
实践中的实现(实例)。创建
Table2
,其中将包含您要检查的所有日期(我只是包含了您在问题示例中描述的内容)创建一个简单的过程来遍历所有
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
提供您问题中预期的结果