是否甚至有可能在Excel中检查列A中的重复项,然后比较列B中的相应date?
我通常不会问任何事情,但我已经打了一堵墙。 我甚至不知道这是否可能。
假设列A是学生列表,列B是每个学生给大学打电话的date列表。 即:
123456 9/21/2013 123456 9/22/2013 123456 9/25/2013 124343 10/10/2014 324242 11/15/2014
所以ID 123456的学生打了三次电话。 但是我特别要计算的是,如果有一个以上的学生打来电话,他打了48个小时内打了多less次电话。 这里的答案是123456在第一次发生的48小时内回叫1次。
任何帮助表示赞赏。
假设你的数据以A2开始(第1行用于标题),然后在C2中尝试下面的公式并将其复制下来:
=COUNTIFS(A:A,A2,B:B,">"&B2,B:B,"<="&B2+2)
这就是说“计算所有的情况下,价值是上校A是当前的学生,并且这个date是在当前行的date和未来2天之间”。
如果有可能在同一天有多个电话,那么这可能会更好:
=COUNTIFS(A:A,A2,B:B,">="&B2,B:B,"<="&B2+2)-1
(从计数中删除每一行与自身的匹配)。
如果48小时需要更加精确,那么您可能需要改变公式以使用小时而不仅仅是几天,但是相同的公式结构应该工作。
微笑,这是可能的:)
首先,将所有学生的ID(不重复)都放入一个集合中(假设他们从Range(“A1”)开始):
Dim studentsID As New Collection For j = 1 To Range("A1").End(xlDown).Row alreadyThere = False For k = 1 To studentsID.Count If Range("A" & j) = studentsID(k) Then alreadyThere = True Exit For End If Next k If alreadyThere = False Then studentsID.Add Range("A" & j).Value 'if student is not there yet, add it to the collection Next j
这个计算一个学生多less时间总共 调用 …因为你问了但我想具体能够计算是否有一个以上的学生呼叫
所以你检查是否有多个来自学生的电话:
For j = 1 To studentsID.Count 'for each student in your list count = 0 For k = 1 To Range("A1").End(xlDown).Row If Range("A" & k) = studentsID(j) Then count = count + 1 'count how many occurrencies there are in column A Next k If count > 1 Then MsgBox "The student " & studentsID(j) & " called us " & count & " times" Next j
而这计算了最后48小时内学生的多less时间
…按照同样的逻辑,你可以计算他在过去48小时内打了多less次:
todayIs = DateSerial(Year(Now()), Month(Now()), Day(Now())) twoDaysAgoWas = todayIs - 2 For j = 1 To studentsID.Count count = 0 For k = 1 To Range("A1").End(xlDown).Row If (Range("A" & k) = studentsID(j)) And (Range("B" & k) >= twoDaysAgoWas) Then count = count + 1 Next k If count > 1 Then MsgBox "The student " & studentsID(j) & " called us " & count & " times during the last 48 hours." Next j
您可以使用这些代码片段来构build所需的输出(例如,而不是使用最有可能将计数器存储在variables中的消息框并将其打印到统计表中)。
注意
如果您最近需要48小时,您可能还需要使用TimeSerial(Hour,Minute,Second)
。