[英]Find Average Interval between Dates in a Pivot Table
I have a spreadsheet with order date data: 我有一个包含订单日期数据的电子表格:
I need to find the average interval in days between each order date. 我需要找到每个订购日期之间的平均间隔天数。 I have to both find a way to get past the blank cells in the row, and also take into account that some clients have 5-10 orders and some clients have 2 orders when calculating my average frequency (interval) between orders.
在计算我的订单之间的平均频率(间隔)时,我必须既找到一种方法来跳过该行中的空白单元格,又要考虑到一些客户有5-10个订单,有些客户有2个订单。
What I have so far: 到目前为止,我有:
Sub DateInt()
Dim CurrentSheet As Worksheet
Dim LastRow As Integer
Dim LastCol As Integer
Dim CurrentRow As Integer
Dim CurrentCol As Integer
Dim GrandT As String
GrandT = InputBox("Which column is the Grand Total in?", "Grand Total Column Letters")
Set CurrentSheet = ActiveWorkbook.ActiveSheet
LastRow = CurrentSheet.Range(GrandT & Rows.Count).End(xlUp).Row
LastCol = CurrentSheet.Cells(4, Columns.Count).End(xlToLeft).Column - 1
For CurrentRow = 5 To LastRow
For CurrentCol = 2 To LastCol
If Not CurrentSheet.Cells(CurrentRow, CurrentCol).Value = "" Then
'Save date
'Find next date in row
'Subtract Dates to get interval and save interval in days
'Save a running average of intervals
'Maybe a running sum (SumDates) and a running divisor (NumOfDates)
Else
Next
'Output average interval (SubDates / NumOfDates) in CurrentSheet.Cells(CurrentRow, LastCol + 1).Value
Next
End Sub
I am having trouble figuring out how to do the loop inside the CurrentCol to LastCol loop: Maybe something like: 我在弄清楚如何在CurrentCol到LastCol循环内执行循环时遇到了麻烦:也许像这样:
NumOfDates = 0
'Loop
'Loop
NumOfDates = NumOfDates + 1
Date & NumOfDates = CurrentCell.Value
If NumOfDates = 1 Then Next
Else
Interval = Date2 - Date1
TtlInterval = TtlInterval + Interval
Date2 = Date1
Next
We've got a winner here: (Even better with update) 我们在这里有一个赢家:(甚至更新时更好)
Sub DateInt()
Dim CurrentSheet As Worksheet
Dim LastRow As Integer
Dim LastCol As Integer
Dim CurrentRow As Integer
Dim CurrentCol As Integer
Dim GrandT As String
Dim DateA As Date
Dim DateB As Date
Dim DateTtl As Integer
Dim DateCount As Integer
Set CurrentSheet = ActiveWorkbook.ActiveSheet
LastRow = CurrentSheet.Range("A" & Rows.Count).End(xlUp).Row - 1
LastCol = CurrentSheet.Cells(4, Columns.Count).End(xlToLeft).Column
Cells(4, LastCol + 1).Value = "Avg Interval"
Cells(4, LastCol + 2).Value = "Days Since Last Order"
Cells(4, LastCol + 3).Value = "Last Order Date"
Cells(4, LastCol + 4).Value = "Last Order v Avg Order"
For CurrentRow = 5 To LastRow
Cells(CurrentRow, LastCol).Value = Date
Cells(CurrentRow, LastCol).NumberFormat = "mm/dd/yy"
DateCount = 0
DateTtl = 0
DateC = DateAdd("d", 20, Date)
For CurrentCol = 2 To LastCol
If Cells(CurrentRow, CurrentCol).Value = "" Then
Else
If DateCount < 1 Then
DateA = Cells(CurrentRow, CurrentCol).Value
Else
DateB = Cells(CurrentRow, CurrentCol).Value
DateTtl = DateDiff("d", DateA, DateB) + DateTtl
If DateValue(DateB) = DateValue(Date) Then
Else
DateA = DateB
End If
End If
DateCount = DateCount + 1
End If
Next CurrentCol
Cells(CurrentRow, LastCol + 1).Value = DateTtl / DateCount
Cells(CurrentRow, LastCol + 1).NumberFormat = "General"
Cells(CurrentRow, LastCol + 2).Value = DateDiff("d", DateA, Date)
Cells(CurrentRow, LastCol + 2).NumberFormat = "General"
Cells(CurrentRow, LastCol + 3).Value = DateA
Cells(CurrentRow, LastCol + 3).NumberFormat = "mm/dd/yy"
Cells(CurrentRow, LastCol + 4).Value = Cells(CurrentRow, LastCol + 1).Value - Cells(CurrentRow, LastCol + 2).Value
Cells(CurrentRow, LastCol + 4).NumberFormat = "#,##0_);[Red](#,##0)"
Next CurrentRow
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.