【问题标题】:Determine start & end of range then loop through all occurrences of that range确定范围的开始和结束,然后遍历该范围的所有出现
【发布时间】:2020-02-25 18:05:17
【问题描述】:

我有一个旧系统的输出,其中列出了数百页。这些按日期分组。由于它是一个旧系统,它会转储输出并将我导入到 A 列中的所有 Excel 中。

我想计算每个日期范围之间有多少实例。

所以我的来源是:请注意日期 Tue Sep 10 08:52:40 2019 是报告运行日期,不需要。

通话日期:08/01/2019

6:47 0:01:04 35905 34312
7:19 0:04:50 33365 34312
7:16 0:12:58 36050 34312
7:45 0:16:06 33206 34312
8:52 0:17:48 33649 34312
9:35 0:02:38 33160 34312
9:29 0:10:10 32735 34312
9:43 0:00:12 34311 34312
9:40 0:04:16 33008 34312
10:19 0:09:18 33805 34312
10:25 0:06:00 32735 34312

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

通话日期:2019 年 8 月 1 日(续)

7:19 0:04:50 33365 34312
7:16 0:12:58 36050 34312
7:45 0:16:06 33206 34312
8:52 0:17:48 33649 34312
9:35 0:02:38 33160 34312
9:29 0:10:10 32735 34312
9:43 0:00:12 34311 34312
9:40 0:04:16 33008 34312
10:19 0:09:18 33805 34312
10:25 0:06:00 32735 34312

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

通话日期:08/03/2019

7:21 0:02:40 37332 34312
7:46 0:00:38 32075 34312
7:49 0:08:02 37606 34312
8:52 0:02:24 33420 34312
8:58 0:01:40 33519 34312
8:59 0:02:20 36039 34312
9:13 0:00:42 35956 34312
9:09 0:04:58 32891 34312
9:19 0:00:18 35338 34312
9:24 0:01:44 33546 34312

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

我认为逻辑是设置两个变量 startRow 和 endRow 与 startRow = "Call Date:" 和 endRow = "Location:"

然后使用 foreach next 循环,遍历 startRow 和 endRow 之间的每个范围,并且 countif 包含“:”

我遇到的问题是如何首先找到范围的开始和范围的结束,但该范围可以重复出现多次。那么是在考虑 For Each Next 循环吗?

````vba
  Set lastRow = .Cells.Find("Location:", .Cells(1, 1), xlValues, xlPart, , xlPrevious)
  Set firstRow = .Cells.Find("Call Date:", .Cells(1, 1), xlValues, xlPart, , xlPrevious)

For Each dateRange In ActiveSheet.Range("firstRow:lastRow")  //not sure how to set and loop through each occurrence of first & last row
COUNTIF(firstRow:lastRow,":") //count the rows containing ":"
Next dateRange

我希望输出为(在 C 列中):

通话日期:08/01/2019 19 次通话//记住这个日期,其他人可以跨越多个页面

通话日期:08/02/2019 10 次通话

通话日期:08/03/2019 6 次通话

等等……

非常感谢!

【问题讨论】:

  • 更好的逻辑如下:对第一个到最后一个单元格执行 foreach 循环,并启动一个计数器。然后在每个单元格中执行Search 以确定其中是否包含“通话日期”。然后为该行设置一个计数器。然后对于你得到的每一个命中,从新的计数器中减去旧的计数器,这就是你的行数。将其写在 C 列中。稍后我将使用功能代码发布答案。
  • 认为Find 是要走的路。将范围变量设置为两个 Find 操作,然后您感兴趣的范围是两者之间的位。

标签: excel loops range


【解决方案1】:

正如我所说,每行计算您感兴趣的行之间的所有行将是我的方法:

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

【讨论】:

  • 哇! Plutian 给我留下了深刻的印象。你从哪里学来的?我有很多研究要做,但非常感谢!
  • 谢谢,很高兴为您提供帮助。我主要是通过反复试验了解到这一点的,并且在这里学习用户的答案比我更聪明、更有经验。
  • 我想知道,如果使用这种逻辑,是否可以找到某个日期之前或之后的所有呼叫。因此,计算 07:00:00 之前和 23:00:00 之后的所有呼叫。基本上,从晚上 11:01 到早上 6:59 的任何电话
  • @Mr80s 是的,这当然是可能的,但是由于这是一个与您的原始问题无关的附加问题,因此最好发布一个包含新要求的新问题。 (I got shouted at)
  • 谢谢你,好点。我创建了一个新帖子stackoverflow.com/q/58629083/11989241
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多