【发布时间】: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
【问题讨论】: