甚至可以在Excel中检查A列中的重复项,然后比较B列中的相应日期?

时间:2015-01-08 23:42:18

标签: excel vba formula

我通常都不会问任何事情,但我已经撞墙了。我甚至都不知道这是否可行。

让我们说A列是学生#的列表,而B列是每个学生给大学打电话的日期列表。即:

123456  9/21/2013
123456  9/22/2013
123456  9/25/2013
124343  10/10/2014
324242  11/15/2014

所以ID为123456的学生要求3次。但我想特别能够计算出,如果有一个以上的学生呼叫,他在原始呼叫的48小时内回拨了多少次。这里的答案是123456在第一次发生的48小时内回拨了1次。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

微笑,它有可能:)

首先,你得到所有的学生' 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

这个学生有多少时间总是被学生打电话 ...因为你问了但是我想特别能够计算出是否有一个以上的学生来电

因此,您要检查学生是否有多个电话:

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小时内有多少学生被录取

......并按照相同的逻辑计算他在过去48小时内打过的次数:

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

您可以使用这些代码片段来构建所需的输出(例如,而不是使用您最有可能希望将计数器存储在变量中的消息框并将其打印到统计信息表中。)

注意

如果您在过去48小时内需要完全,您可能还需要使用TimeSerial(Hour,Minute,Second)功能。

答案 1 :(得分:1)

假设您的数据在A2中开始(第1行用于标题),请在C2中尝试以下公式并将其复制下来:

=COUNTIFS(A:A,A2,B:B,">"&B2,B:B,"<="&B2+2)

表示&#34;计算值为col A的所有情况,即当前学生的日期,以及当前行的日期和未来的2天之间的日期&#34;。

如果可能在同一天有多个电话,那么这可能会更好:

=COUNTIFS(A:A,A2,B:B,">="&B2,B:B,"<="&B2+2)-1

-1从计数中删除每行与自身的匹配。

如果48小时需要更精确,那么您可能需要改变公式以使用小时而不是几天,但相同的公式结构应该有效。