简体   繁体   English

使用 Excel VBA Dir() 函数循环并打开文件目录提前退出

[英]Looping through and opening a directory of files with Excel VBA Dir() function quitting early

Posting this here in case someone is having similar trouble.在这里发布以防万一有人遇到类似的问题。 Not really looking for an answer, just posting in case someone is in need of a solution to a similar problem.不是真的在寻找答案,只是发布以防有人需要解决类似问题。 If someone has some experience as to exactly what is going on or a way to further mitigate the problem I will check mark their answer.如果有人对正在发生的事情或进一步缓解问题的方法有一些经验,我会勾选他们的答案。

I needed to loop through a directory of .csv files, retrieve a piece of information from each one and compile in a new .xlsx file.我需要遍历一个 .csv 文件目录,从每个文件中检索一条信息,然后编译成一个新的 .xlsx 文件。 My solution was based on this thread:我的解决方案基于此线程:

VBA code to loop through files in a folder 遍历文件夹中文件的VBA代码

The trouble I WAS having was that the Dir() function was bailing well before looping through all the files (I had 500 of them...it was stopping at random places, sometimes 18th, sometimes 90th, sometimes 97th).我遇到的问题是 Dir() 函数在循环遍历所有文件之前很好地保释(我有 500 个文件......它在随机位置停止,有时在第 18 个,有时在第 90 个,有时在第 97 个)。 It took me a while to figure out what was going on.我花了一段时间才弄清楚发生了什么。 Apparently, while this function was executing, other things I was doing (I assume it was a Word document I was writing) was telling the macro to stop with no error codes.显然,当这个函数正在执行时,我正在做的其他事情(我假设它是我正在编写的 Word 文档)正在告诉宏停止并且没有错误代码。 It was just exiting out of the loop (I was using a while -> wend loop) leaving the last document the function visited opened and not executing the close statement.它只是退出循环(我正在使用 while -> wend 循环),留下函数访问的最后一个文档打开而不执行 close 语句。 I confirmed this by shutting everything down and running the macro without anything else open and it finished executing.我通过关闭所有内容并在没有打开任何其他内容的情况下运行宏来确认这一点,并完成了执行。

So if you're having trouble with the Dir() function stopping execution before checking every file in a directory, close other microsoft applications.因此,如果在检查目录中的每个文件之前 Dir() 函数停止执行时遇到问题,请关闭其他微软应用程序。 They may be interfering.他们可能会干扰。

To avoid the Dir bug, collect the files first and then process them.为避免Dir错误,请先收集文件,然后再处理它们。 Do not do any office related stuff between the calls to Dir .不要在调用Dir之间做任何与办公室相关的事情。

Option Explicit

' Get the files specified
Function GetFiles(MyFolder As String) As Variant
    Dim MyFile As Variant, Files As Variant
    Dim NumFiles As Long, Idx As Long
    
    ' Collect files only and ignore folders
    MyFile = Dir(MyFolder)
    NumFiles = 0
    Do While MyFile <> ""
        NumFiles = NumFiles + 1
        If NumFiles = 1 Then
            ReDim Files(1 To 1)
        Else
            ReDim Preserve Files(1 To NumFiles)
        End If
        Files(NumFiles) = MyFile
        MyFile = Dir()
    Loop
    GetFiles = Files
End Function

Sub ProcessFiles(MyFolder As String)
    Dim MyFile As Variant
    Dim Files As Variant
    Dim Idx As Long
    
    Files = GetFiles(MyFolder)
    If Not IsEmpty(Files) Then
        For Idx = LBound(Files) To UBound(Files)
            MyFile = Files(Idx)
            ' Process the file here
            Debug.Print MyFile
        Next Idx
    Else
        Debug.Print "No files found for Dir(""" & MyFolder & """)"
    End If
End Sub

Sub TestProcessFiles()
    ProcessFiles "C:\windows\*.*"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM