如何让 vba 中的每个循环跳到下一次迭代?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17241257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:48:59  来源:igfitidea点击:

How to have for each loop in vba skip to next iteration?

vbaforeachif-statement

提问by Kristof I.

I got a file reader from excelexperts.com that lists the files within a folder (and has an option to include subfolders). I decided to tweak the code to skip "Thumbs.db" files.

我从 excelexperts.com 获得了一个文件阅读器,它列出了文件夹中的文件(并且可以选择包含子文件夹)。我决定调整代码以跳过“Thumbs.db”文件。

I've actually got the code working with a GoTo statement as a last resort but I know this is considered bad programming and was wondering what the proper code would be.

我实际上已经让代码使用 GoTo 语句作为最后的手段,但我知道这被认为是糟糕的编程,并且想知道正确的代码是什么。

Dim iRow

Sub ListFiles()
    iRow = 11
    Call ListMyFiles(Range("C7"), Range("C8"))
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
    Set MyObject = New Scripting.FileSystemObject
    Set mySource = MyObject.GetFolder(mySourcePath)
    On Error Resume Next
    For Each myFile In mySource.Files
        **If myFile.Name = "Thumbs.db" Then
            GoTo nextone
        Else**
            iCol = 2
            Cells(iRow, iCol).Value = myFile.Path
            iCol = iCol + 1
            Cells(iRow, iCol).Value = myFile.Name
            iCol = iCol + 1
            Cells(iRow, iCol).Value = myFile.Size
            iCol = iCol + 1
            Cells(iRow, iCol).Value = myFile.DateLastModified
            iRow = iRow + 1
        **End If**
**nextone:**
    Next
    If IncludeSubfolders Then
        For Each mySubFolder In mySource.SubFolders
            Call ListMyFiles(mySubFolder.Path, True)
        Next
    End If
End Sub

The parts I added in are encapsulated in double stars.

我加进去的部分是用双星封装的。

In my previous attempt, I had added in the following code:

在我之前的尝试中,我添加了以下代码:

If myFile.Name = "Thumbs.db" Then
    Next
Else
    rest of the code here
End If

But I got the "Next without For" error. That's why I went with the GoTo statement that I'm hoping to replace with something better.

但是我收到了“Next without For”错误。这就是为什么我使用 GoTo 语句的原因,我希望用更好的东西替换它。

采纳答案by Mike J

Just change the logic of your If statment around to:

只需将 If 语句的逻辑更改为:

For Each myFile In mySource.Files
    If myFile.Name <> "Thumbs.db" Then
        iCol = 2
        Cells(iRow, iCol).Value = myFile.Path
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Name
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Size
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.DateLastModified
        iRow = iRow + 1
    End If
Next

If you are going to have additional logic on how you skip files in the future, you should move the detection logic to a separate method.

如果您打算在将来如何跳过文件有其他逻辑,则应将检测逻辑移至单独的方法。

For Each myFile In mySource.Files
    If ShouldProcessFile(myFile) Then
        iCol = 2
        Cells(iRow, iCol).Value = myFile.Path
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Name
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.Size
        iCol = iCol + 1
        Cells(iRow, iCol).Value = myFile.DateLastModified
        iRow = iRow + 1
    End If
Next

Function ShouldProcessFile(file)
    ShouldProcessFile = True
    If file.Name = "Thumbs.db" Then ShouldProcessFile = False
End Function