0

我想用 excel VBA 更新当前子文件夹中的文件。第一步是在子文件夹中查找文件名。把它们都列在另一张纸上,这样我就可以记录下来。复制并用新文件覆盖文件,因此我的所有文件夹和子文件夹都将使用新文件进行更新。

source
D:\home
destination
D:\dest\cus1\...

我目前正在使用下面的代码,但我至少需要改进 for 循环或任何新算法。你能帮忙吗?

Sub sbCopyingAllExcelFiles()

    Dim FSO
    Dim sFolder As String
    Dim dFolder As String

    sFolder = "c:\Users\osmanerc\Desktop\STATUS\" ' change to match the source folder path
    dFolder = "\\manfile\ELEKTRONIK\MUSTERI DESTEK\ECN management\" ' change to match the destination folder path
    Set FSO = CreateObject("Scripting.FileSystemObject")

    If Not FSO.FolderExists(sFolder) Then
        MsgBox "Source Folder Not Found", vbInformation, "Source Not Found!"
    ElseIf Not FSO.FolderExists(dFolder) Then
        MsgBox "Destination Folder Not Found", vbInformation, "Destination Not Found!"
    Else
        FSO.CopyFile (sFolder & "\*.xl*"), dFolder
        MsgBox "Successfully Copied All Excel Files to Destination", vbInformation, "Done!"
    End If
End Sub
4

1 回答 1

0

因此,这应该能够从您的源中复制与该Like sFolder & "\*.xl*"模式匹配的所有文件。如果您有更多文件夹可供使用,则可以添加更多呼叫。

Sub sbCopyingAllExcelFiles()

    Call SafeCopy("c:\Users\osmanerc\Desktop\STATUS\", "\\manfile\ELEKTRONIK\MUSTERI DESTEK\ECN management\")
    'Call SafeCopy("another source folder", "another destination folder")
    'Add more function calls as necessary

End Sub

Function SafeCopy(ByVal sFolder As String, ByVal dFolder As String)

    Dim count As Integer

    Dim FSO As Object
    Dim Folder As Object
    Dim File As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")

    If Not FSO.FolderExists(sFolder) Then
        MsgBox "Source Folder Not Found: " & vbCrLf & sFolder, vbInformation, "Source Not Found!"
        Exit Function
    ElseIf Not FSO.FolderExists(dFolder) Then
        MsgBox "Destination Folder Not Found: " & vbCrLf & dFolder, vbInformation, "Destination Not Found!"
        Exit Function
    Else
        Set Folder = FSO.GetFolder(sFolder)

        For Each File In Folder.Files
            If File.Name Like sFolder & "\*.xl*" Then
                FSO.CopyFile File.path, dFolder
                count = count + 1
            End If
        Next

        MsgBox "Copied " & count & "files to destination", vbInformation, "Copy Successful"
    End If

End Function
于 2018-06-05T12:43:31.187 回答