【问题标题】:VBA exiting loop at the last true conditionVBA在最后一个真实条件下退出循环
【发布时间】:2016-09-27 19:00:14
【问题描述】:

我正在构建一个宏,该宏将在文件夹中查找特定报告类型的所有 xls 文件,并将它们与整个路径一起列在工作表中。然后它按升序对它们进行排序并找到最新的文件,打开该文件并将其中的信息复制到一个空白文件中,作为聚合来自多种报告的最新数据的过程。

问题是在找到最新文件后退出循环:

Dim i As Variant
Dim myarray() As Integer
Dim myarray2() As Variant

i = 0

For Each cell In Range("C1:C" & x)
    If InStr(1, cell, "proof") Then
        i = i + 1
        Debug.Print i & " " & cell.value
        ReDim myarray(i)
        ReDim Preserve myarray2(cell)
    End If

    Do
    Loop Until InStr(1, cell, "proof")
Next cell

这是一个试用代码 - 它会在指定文件夹中查找文件名中包含 "proof" 的所有文件,然后在即时窗口中打印它们。动态数组存储每个文件名,直到循环结束,Do Until 循环例程应该在最后一个检查为真的条件下停止它——它将被打开并从中复制信息的文件。问题是Do Until 循环锁定了整个例程,因为第一次迭代是真的会满足它,我不知道如何让它退出它应该在的地方......

Do-While 循环放在同一个位置时只会自行循环。

【问题讨论】:

  • 我不确定您如何确定“最后一个条件”,但 do 循环是没有意义的,因为它会为每个 cell 重新启动,并且它与上面的 if 具有相同的条件. if 中的代码也没有意义。看来您想删除do 循环,更改if 的内部以实际将值存储在myarray2 中,让for 正常完成,然后使用myarray2 的最后一个元素。
  • do...while 中的测试单元格永远不会改变。因此,如果单元格不是“证明”,它将永远循环。
  • @GSerit,就是这样——最后一个“证明”文件存储在 myarray2 中,但在下一次迭代中,它会被不符合条件的文件取代,因为宏会测试每个文件在列表中。所以,我的难题是保留文件名中包含“证明”的最后一个文件,而循环遍历列表的其余部分。这就是 Do-Until 循环的基本原理......
  • 精简版,确实。我不确定 do-while 循环的表达式应该是什么。

标签: vba excel


【解决方案1】:

不要真正达到你的全部目标。但你可以先试试这个

Option Explicit

Sub main()
    Dim i As Variant
    Dim myarray() As String
    Dim cell As Range
    Dim firstAddress As String
    Dim nFound As Long

    With Range("C1", Cells(Rows.Count, "C").End(xlUp)) '<--| reference column "C" cells from row 1 down to last non empty one
        nFound = WorksheetFunction.CountIf(.Cells, "*proof*") '<--| count occurrences of wanted substring
        If nFound = 0 Then Exit Sub '<--| exit if no occurrences
        ReDim myarray(1 To nFound) '<--| size your array to match occurrences
        Set cell = .Find(what:="proof", after:=.Cells(.Rows.Count, 1), LookIn:=xlValues, lookat:=xlPart, searchdirection:=xlNext) '<--| find first occurrence
        firstAddress = cell.Address '<--| store first occurrence address
        Do
            i = i + 1 '<-- update array index
            myarray(i) = cell.Value '<--| update array current index content
            Set cell = .FindNext(cell) '<--| search for next occurrence
        Loop While cell.Address <> firstAddress '<--| exit if occurrence wrapped back to the first one
    End With
End Sub

【讨论】:

  • CountIf 绝对比测试范围内的部分字符串要好。非常酷的方法。按照您的示例,整个 Do-While 循环业务也更有意义。谢谢你,user3598756!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
相关资源
最近更新 更多