【问题标题】:Looping through files and protect worksheet by opening and saving one at a time通过一次打开和保存一个文件来循环文件并保护工作表
【发布时间】:2018-01-09 09:54:03
【问题描述】:

我正在处理 200 多个 Excel 文件,我需要保护我的 Excel 工作表,然后才能将其发送给其他公司。下面的代码设法工作,但问题是它一直打开文件夹中的所有 excel 文件,直到它耗尽内存来处理,我还必须手动保存所有打开的文件

有没有一种方法可以让代码一次循环一个文件夹中的 excel 文件并在继续处理下一个 excel 文件之前将其保存?

Sub LoopThroughFiles()

    FolderName = "C:\Users\Desktop\PROJECTS\PROJECT FILE\A"
    If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
    Fname = Dir(FolderName & "*.xlsx")

    'loop through the files
    Do While Len(Fname)

        With Workbooks.Open(FolderName & Fname)

           ' here comes the code for the operations on every file the code finds

        ActiveSheet.Protect "password", True, True


        End With

        ' go to the next file in the folder
        Fname = Dir

    Loop

End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:
    Sub LoopThroughFiles()
    
        FolderName = "C:\Users\Desktop\PROJECTS\PROJECT FILE\A"
        If Right(FolderName, 1) <> Application.PathSeparator Then FolderName = FolderName & Application.PathSeparator
        Fname = Dir(FolderName & "*.xlsx")
    
        'loop through the files
        Do While Len(Fname)
    
            With Workbooks.Open(FolderName & Fname)
    
               ' here comes the code for the operations on every file the code finds
    
            ActiveSheet.Protect "password", True, True
            .save
            .close
    
            End With
    
            ' go to the next file in the folder
            Fname = Dir
    
        Loop
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 2010-09-14
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多