简体   繁体   English

VBA 循环遍历一个目录中的文件,在另一个目录中另存为 csv,如果文件存在则跳过

[英]VBA Loop through files in a directory, save as csv in another directory, skip if file exists

I have a bit of code that loops through a bunch of files in a folder, runs a macro on each of them, and then saves them as a.csv file in a different folder.我有一些代码循环遍历文件夹中的一堆文件,在每个文件上运行一个宏,然后将它们保存为不同文件夹中的 .csv 文件。 The process runs fine with if the destination csv folder is empty.如果目标 csv 文件夹为空,则该过程运行良好。 What I want to do is skip the process if the.csv file already exists.如果 .csv 文件已经存在,我想要做的是跳过该过程。 The problem with the code below, is that the Filename = Dir() returns a null value and the loop ends if the.csv file exists.下面代码的问题在于 Filename = Dir() 返回 null 值,如果存在 .csv 文件,则循环结束。 So how do I continue looping through the other files in the first folder?那么如何继续遍历第一个文件夹中的其他文件呢?

Sub ProcessFiles()
Dim Filename, Pathname, strFileExists As String
Dim wb As Workbook

Application.ScreenUpdating = False

Pathname = ActiveWorkbook.Path & "\Files\"
Filename = Dir(Pathname & "*.xlsx")
Do While Filename <> ""
    Set wb = Workbooks.Open(Pathname & Filename)
    length = Len(ActiveWorkbook.Name)
    Name = Left(ActiveWorkbook.Name, length - 5)
    CSVName = ActiveWorkbook.Path & "\CSV Files\" & Name & ".csv"
    strFileExists = Dir(CSVName)

    If strFileExists = "" Then
        Transform wb 'Run Transform function
        wb.SaveAs Filename:=CSVName, FileFormat:=xlCSVMSDOS, CreateBackup:=False
        wb.Close SaveChanges:=False
        Filename = Dir()
    Else
        wb.Close SaveChanges:=False
        Filename = Dir()
    End If
Loop
End Sub

I think braX is right: the problem is you are using Dir twice.我认为 braX 是对的:问题是您使用 Dir 两次。 This seems to be working for me:这似乎对我有用:

Sub ProcessFiles()
    Dim Filename, Pathname, strFileExists As String
    Dim wb As Workbook
    Dim IntFileNumber As Integer
    Dim IntCounter01 As Integer
    Dim Length As Byte
    Dim Name As String
    Dim CSVName As String

    Application.ScreenUpdating = False

    Pathname = ActiveWorkbook.Path & "\Files\"
    Filename = Dir(Pathname & "*.xlsx")

    Do While Filename <> ""
        Set wb = Workbooks.Open(Pathname & Filename)
        Length = Len(ActiveWorkbook.Name)
        Name = Left(ActiveWorkbook.Name, Length - 5)
        CSVName = ActiveWorkbook.Path & "\CSV Files\" & Name & ".csv"
        strFileExists = Dir(CSVName)


        If strFileExists = "" Then
            Transform wb 'Run Transform function
            wb.SaveAs Filename:=CSVName, FileFormat:=xlCSVMSDOS, CreateBackup:=False
            wb.Close SaveChanges:=False
            Filename = Dir(Pathname & "*.xlsx")
            IntFileNumber = IntFileNumber + 1
            For IntCounter01 = 1 To IntFileNumber
                Filename = Dir()
            Next
        Else
            wb.Close SaveChanges:=False
            Filename = Dir(Pathname & "*.xlsx")
            IntFileNumber = IntFileNumber + 1
            For IntCounter01 = 1 To IntFileNumber
                Filename = Dir()
            Next
        End If
    Loop

End Sub

Basically i reset the Filename and re-play Dir as many time as needed to reach the wanted file.基本上我重置文件名并根据需要多次重新播放目录以到达想要的文件。

I've added some declarations too.我也添加了一些声明。 You might also want to turn true the ScreenUpdating at the end of the subroutine, but that's up to you.您可能还希望在子例程结束时将 ScreenUpdating 设为 true,但这取决于您。

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

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