[英]VBA code to loop through files in a folder
我正在寻找一种方法来自动化在单独的子文件夹中打开 1300 个单独的 Excel 文件并在每个文件中进行相同更改的过程。 更改将针对每个文件的同一选项卡上的同一单元格。 我几乎完成了与 Workbook.open 有问题的代码,并确保它选择了正确的 excel 文件。 我在下面列出了我的代码。 如果您需要,我还有更多信息。 谢谢您的帮助!
我需要访问的 Excel 文件位于父文件夹的子文件夹中/需要更新的 Excel 文件全部以 List.xlsx 结尾
Sub LoopFilesInFolder()
Dim folderName As String
Dim FSOLibrary As FileSystemObject
Dim FSOFolder As Object
Dim FSOFile As Object
'Set the file name to a variable
folderName = "\\net\fs1\users\alex.wood\Desktop\TC Excel Test and Creation Files\VBA TEST FOLDER"
'Set all the references to the FSO Library
Set FSOLibrary = New FileSystemObject
Set FSOFolder = FSOLibrary.GetFolder(folderName)
Set FSOFile = FSOFolder.Files
'Use For Each loop to loop through each file in the folder
For Each FSOFile In FSOFile
Workbooks.Open Filename:=Dir("\\net\fs1\users\alex.wood\Desktop\TC Excel Test and Creation Files\VBA TEST FOLDER\*list*")
Range("G1").Select
ActiveCell.FormulaR1C1 = "New Thin Client"
ActiveWorkbook.Save
ActiveWindow.Close
Next
'Release the memory
Set FSOLibrary = Nothing
Set FSOFolder = Nothing
Set FSOFile = Nothing
End Sub
你可以这样做
Sub LoopFilesInFolder()
'Improve performance
Application.ScreenUpdating = True
mypath = "\\net\fs1\users\alex.wood\Desktop\TC Excel Test and Creation Files\VBA TEST FOLDER\"
myfile = Dir(mypath & "*.xls*")
Do While myfile <> ""
Workbooks.Open mypath & myfile
Workbooks(myfile).Activate
ActiveSheet.Range("G1").Value = "New Thin Client"
Workbooks(myfile).Save
Workbooks(myfile).Close
myfile = Dir
Loop
Application.ScreenUpdating = False
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.