【发布时间】:2014-11-11 22:26:12
【问题描述】:
我有一个包含订单日期数据的电子表格:
我需要找到每个订单日期之间的平均天数。在计算我的订单之间的平均频率(间隔)时,我既要找到一种方法越过行中的空白单元格,还要考虑到有些客户有 5-10 个订单,有些客户有 2 个订单。
到目前为止我所拥有的:
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
我无法弄清楚如何在 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
【问题讨论】:
-
感谢您解决问题。我会投票重新提出您的问题,但您仍应添加更多细节。您是否尝试过解决您在代码中评论过的那些个别问题?
标签: vba date excel pivot-table