VBA在最后一个真实条件下退出循环

VBA在最后一个真实条件下退出循环,vba,excel,Vba,Excel,我正在构建一个宏,它将在文件夹中查找特定报告类型的所有xls文件,并在工作表中列出它们的整个路径。然后,它按升序对它们进行排序,并查找最新的文件,该文件将被打开,其中的信息将复制到一个空白文件中,作为一个过程,该过程聚合了几种报告中的最新数据 问题是在找到最新文件后退出循环: Dim i As Variant Dim myarray() As Integer Dim myarray2() As Variant i = 0 For Each cell In Range("C1:C" &

我正在构建一个宏,它将在文件夹中查找特定报告类型的所有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
循环例程应该在最后一个检查为true的条件下停止它-它是随后将打开并从中复制信息的文件。问题是,
Do-Until
循环锁定了整个例程,因为第一次迭代是正确的,可以满足它,我不知道如何让它在应该退出的地方退出


一个
Do-While
循环如果放在同一个位置,就会自动循环。

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

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
选项显式
副标题()
Dim i作为变体
Dim myarray()作为字符串
暗淡单元格作为范围
将第一个地址设置为字符串
长

使用Range(“C1”,单元格(Rows.Count,“C”).End(xlUp))并不能真正实现您的全部目标。但你可以先试试这个

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
选项显式
副标题()
Dim i作为变体
Dim myarray()作为字符串
暗淡单元格作为范围
将第一个地址设置为字符串
长

对于Range(“C1”,Cells(Rows.Count,“C”).End(xlUp)),我不确定如何确定“最后一个条件”,但
do
循环是没有意义的,因为它会为每个
单元格重新启动,并且它的条件与上面的
if
相同。
if
中的代码也没有意义。似乎您希望删除
do
循环,更改
的内部结构,如果
将值实际存储在
myarray2
中,则让
for
正常完成,然后使用
myarray2
的最后一个元素。
中被测试的单元格……而
从不更改。因此,如果单元格不是“证明”,它将永远循环。@GSerit,就是这样-最后一个“证明”文件存储在myarray2中,但在下一次迭代中,它将被一个不符合条件的文件取代,因为宏将测试列表中的每个文件。因此,我的难题是保留文件名中最后一个有“证明”的文件,而循环遍历列表的其余部分。这就是Do-Until循环的基本原理……实际上是litelite。我不确定do while循环的表达式应该是什么。我不确定如何确定“最后一个条件”,但
do
循环是没有意义的,因为它为每个
单元格重新启动,并且它的条件与上面的
if
相同。
if
中的代码也没有意义。似乎您希望删除
do
循环,更改
的内部结构,如果
将值实际存储在
myarray2
中,则让
for
正常完成,然后使用
myarray2
的最后一个元素。
中被测试的单元格……而
从不更改。因此,如果单元格不是“证明”,它将永远循环。@GSerit,就是这样-最后一个“证明”文件存储在myarray2中,但在下一次迭代中,它将被一个不符合条件的文件取代,因为宏将测试列表中的每个文件。因此,我的难题是保留文件名中最后一个有“证明”的文件,而循环遍历列表的其余部分。这就是Do-Until循环的基本原理……实际上是litelite。我不确定do while循环的表达式应该是什么。CountIf肯定比测试范围内的字符串部分要好。这是一个非常酷的方式。整个Do-While循环业务也更符合您的示例。谢谢你,用户3598756!CountIf肯定比测试范围内的字符串部分要好。这是一个非常酷的方式。整个Do-While循环业务也更符合您的示例。谢谢你,用户3598756!