计算超出日期范围的时间

时间:2019-10-30 15:45:12

标签: excel range countif

为了使新问题与先前的问题分开,我将其作为另一个问题。

根据下面的信息(全部在A列中),我希望能够计算出某个时间(或两次)之间某个日期有多少个来电。例如,使用第一组数字(即6:47

),在08/01/2019上有多少次呼叫,而不是从07:00到23:00。

我的源文件的格式混乱,但是下面的数字表示:

2019年10月30日星期三11:42:11地点:一些。放置页面1

          Call Details by Date and Time

Call Date: 01/01/2019
____________________________________________________________________________________________________________________________________________________
Start Costed   Call       Call        Access Dialed Number               Call Cost  Call          Real     Authorization   Account Code Billing Code
Time  Duration Origin     Destination  Code                                         Charge     Extension       Code                                 
_____ ________ __________ ___________ ______ ___________________________ __________ __________ __________ ________________ ____________ ____________
 1:24 00:05:12      34320      34312                               34312      $0.00      $0.00      34320                                           
11:11 00:02:46      33314      34312                               34312      $0.00      $0.00      33314                                           
19:41 00:00:50      36424      34312                               34312      $0.00      $0.00      36424                                           
20:07 00:03:28      34227      34312                               34312      $0.00      $0.00      34227                                           
21:06 00:09:00      36335      34312                               34312      $0.00      $0.00      36335                                           
21:34 00:01:54      37641      34312                               34312      $0.00      $0.00      37641                                           
Wed Oct 30 11:42:11 2019   Location: Some. Place                        Page 2

          Call Details by Date and Time

Call Date: 01/02/2019
____________________________________________________________________________________________________________________________________________________
Start Costed   Call       Call        Access Dialed Number               Call Cost  Call          Real     Authorization   Account Code Billing Code
Time  Duration Origin     Destination  Code                                         Charge     Extension       Code                                 
_____ ________ __________ ___________ ______ ___________________________ __________ __________ __________ ________________ ____________ ____________
 4:15 00:09:00      36335      34312                               34312      $0.00      $0.00      36335                                           
 4:46 00:03:30      32970      34312                               34312      $0.00      $0.00      32970                                           
 7:12 00:00:54      33022      34312                               34312      $0.00      $0.00      33022                                           
 7:21 00:03:04      33655      34312                               34312      $0.00      $0.00      33655                                           
21:02 00:00:24      33277      34312                               34312      $0.00      $0.00      33277                                           
21:19 00:02:44      37606      34312                               34312      $0.00      $0.00      37606                                           
Wed Oct 30 11:42:11 2019   Location: Some. Place                        Page 3

          Call Details by Date and Time

Call Date: 01/03/2019
____________________________________________________________________________________________________________________________________________________
Start Costed   Call       Call        Access Dialed Number               Call Cost  Call          Real     Authorization   Account Code Billing Code
Time  Duration Origin     Destination  Code                                         Charge     Extension       Code                                 
_____ ________ __________ ___________ ______ ___________________________ __________ __________ __________ ________________ ____________ ____________
 5:52 00:01:26      33322      34312                               34312      $0.00      $0.00      33322                                           
 8:09 00:05:50      34229      34312                               34312      $0.00      $0.00      34229                                           
 9:28 00:02:48      33952      34312                               34312      $0.00      $0.00      33952                                           

2019年9月10日星期二08:52:40位置:第4页

因此,此代码是一种用于计算日期中所有呼叫的方式(介于@Plutian的“呼叫日期”和“位置”之间,并且效果很好!

Sub counter()
Dim cel As Range
Dim i As Integer
Dim lastr As Integer
Dim calldate As String

i = 0
lastr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row 'determine last row of data

For Each cel In Sheet1.Range("A1:A" & lastr) 'start loop

If InStr(cel.Value, "Call Date") Then 'check if your value is "Call date" indicating start of data
    If calldate = "" Then 'check if this is the first loop
        calldate = cel.Value 'set calldate to the current loop.
            Else 'if not first loop, write the current calldate + counter to the next available blank cell
            Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = calldate & " " & i
            i = 0 'reste counter
            calldate = cel.Value 'save next calldate value
    End If
    Else
        If cel <> "" Then 'test if cell is blank, skip if it is
            If InStr(cel, "Location") Then 'test if cell holds "Location, indicating it is not data. Skip if it is
                Else
                i = i + 1 'increase counter if part of data
            End If
        End If
End If
Next cel
Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = calldate & " " & I & " calls" 'Write current data at end of loop.
End Sub

希望输出为:(下一个未使用的列)

通话日期:2019年1月8日超出范围1的通话

通话日期:2019年8月2日通话超出范围0

通话日期:2019年8月3日超出范围2的通话

谢谢!

1 个答案:

答案 0 :(得分:0)

因为这是我的代码开头,因此很容易适应您的需求。

我在这里所做的事情是添加了第二个计数器,该计数器在每次发现超出指定时间的点击时都会增加。为了简化起见,我将两个结果串联在一起,因此您不必牺牲任何一个。

Sub counter()
Dim cel As Range
Dim i As Integer, j As Integer
Dim lastr As Integer
Dim calldate As String

i = 0
lastr = Sheet1.Range("A" & Rows.Count).End(xlUp).Row 'determine last row of data

For Each cel In Sheet1.Range("A1:A" & lastr) 'start loop

If InStr(cel.Value, "Call Date") Then 'check if your value is "Call date" indicating start of data
    If calldate = "" Then 'check if this is the first loop
        calldate = cel.Value 'set calldate to the current loop.
            Else 'if not first loop, write the current calldate + both counters to the next available blank cell
            Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = calldate & " " & i & " calls, of which " & j & " outside hours"
            i = 0 'reset counter
            j = 0 'reset counter
            calldate = cel.Value 'save next calldate value
    End If
    Else
        If cel <> "" Then 'test if cell is blank, skip if it is
         If IsDate(Left(cel.Value, 5)) Then 'test if first 5 characters of cell is a valid time.
                If TimeValue(Left(cel.Value, 5)) < "07:00:00" Or TimeValue(Left(cel.Value, 5)) > "23:00:00" Then 'test if call is earlier than 07:00 or later than 23:00
                    j = j + 1 'increase counter if outside that time
                End If
                i = i + 1 'increase counter if part of data
            End If
        End If
End If
Next cel
Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = calldate & " " & i & " calls, of which " & j & " outside hours" 'Write current data at end of loop.
End Sub