需要在多个文件

时间:2015-11-12 23:52:21

标签: excel vba excel-vba

此宏应该遍历所有文件夹/子文件夹,查找所有Excel文件并从特定单元格中删除空格。

问题是,当我运行它时,它要求重新打开当前的XLS文档,如果我接受,它会重新打开,但宏会停止并消失。

代码有什么问题?我想我不应该打开这样的文件,无论如何更新单元格值而不打开它们或以不同方式打开它们是什么? 代码:

Sub Main()
    Call RecursiveFolders("C:\Users\Administrador\Desktop\Folder")
End Sub

Sub RecursiveFolders(ByVal MyPath As String)
    Dim FileSys As Object
    Dim objFolder As Object
    Dim objSubFolder As Object
    Dim objFile As Object
    Dim wkbOpen As Workbook
    Dim book As Excel.Workbook

    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set objFolder = FileSys.GetFolder(MyPath)

    Application.ScreenUpdating = False

    For Each objSubFolder In objFolder.SubFolders
        For Each objFile In objSubFolder.Files
            Set wkbOpen = Workbooks.Open(Filename:=objFile)
            '//For each file do that
            Dim ws As Worksheet
            Dim tmp

            For Each ws In Worksheets
                ws.Range("E6") = Replace(ws.Range("E6"), " ", "", 1, -1)
            Next
            wkbOpen.Close savechanges:=True
        Next

        Call RecursiveFolders(objSubFolder.Path)
    Next

    Application.ScreenUpdating = False
End Sub

1 个答案:

答案 0 :(得分:1)

我想要点XD

  

如果你用宏打开文件,它需要关闭它(退出宏)...确保它没有用宏(或具有相同名称的WB)打开WB&lt; < / p>