我試圖對一些excel工作簿(超過20個)進行大量更改.每個工作簿包含大約16個單獨的工作表,我想編寫一個腳本,它將循環(huán)遍歷每個工作簿,工作表包含內(nèi)部并寫入/修改我需要的單元格.我需要保留所有字符串驗證,宏和格式.所有工作簿都是2007年的格式.
我已經(jīng)看過python excel庫和PHPexcel,但是宏,按鈕,公式,字符串驗證和格式化,并且在編寫新工作簿時不會保留.有沒有一種簡單的方法可以做到這一點,或者我是否必須單獨打開每個工作簿并提交更改.我試圖避免在VBscript中創(chuàng)建一個宏,并且必須單獨打開每個工作簿以提交我需要的更改.
解決方法:
我避免使用像瘟疫這樣的多個工作簿這是一個痛苦,如果這是一個持續(xù)的要求,那么我建議回顧你的工作簿設(shè)計,看看你是否可以合并回一個工作簿.我經(jīng)??吹矫總€工作簿都按月保存,當(dāng)時他們應(yīng)該有一個工作簿,其中一個工作表包含原始數(shù)據(jù),每行代表一個月,然后是另一個顯示用于查找用戶選擇的原始數(shù)據(jù)的工作表.這是一個非常大的概括,你很可能處于完全不同的情況.
如果它一次性 – 我知道它不是你想要的,但我認(rèn)為你最好使用VBA循環(huán)工作簿.像(未經(jīng)測試)的東西:
Excel 2003:
Sub AdjustMultipleFiles()Dim lCount As LongDim wbLoopBook As WorkbookDim wsLoopSheet As WorksheetWith Application .ScreenUpdating = False: .DisplayAlerts = False: .EnableEvents = FalseEnd WithWith Application.FileSearch .NewSearch '// Change path to suit .LookIn = "C:\MyDocuments" '// ALL Excel files .FileType = msoFileTypeExcelWorkbooks '// Uncomment if file naming convention needed '.Filename = "Book*.xls" '// Check for workbooks If .Execute > 0 Then '// Loop through all. For lCount = 1 To .FoundFiles.Count '// Open Workbook x and Set a Workbook variable to it Set wbLoopBook = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0) '// Loop through all worksheets For Each wsLoopSheet In wbLoopBook.Worksheets '//Update your worksheets here... Next wsLoopSheet '// Close Workbook & Save wbLoopBook.Close SaveChanges:=True '// Release object variable Set wbLoopBook = Nothing Next lCount End IfEnd WithWith Application .ScreenUpdating = True: .DisplayAlerts = True: .EnableEvents = TrueEnd WithEnd Sub
對于EXCEL 2007:
Sub AdjustMultipleFiles() Dim sFileName As String Dim wbLoopBook As Workbook Dim wsLoopSheet As Worksheet With Application .ScreenUpdating = False: .DisplayAlerts = False: .EnableEvents = False End With '// Change path to suit ChDir "C:\Documents" '// ALL Excel 2007 files sFileName = Dir("*.xlsx") Do While sFileName <> "" '// Open Workbook x and Set a Workbook variable to it Set wbLoopBook = Workbooks.Open(Filename:=sFileName, UpdateLinks:=0) '// Loop through all worksheets For Each wsLoopSheet In wbLoopBook.Worksheets '//Update your worksheets here... Next wsLoopSheet '// Close Workbook & Save wbLoopBook.Close SaveChanges:=True '// Release object variable Set wbLoopBook = Nothing '//Next File sFileName = Dir '//End Loop Loop With Application .ScreenUpdating = True: .DisplayAlerts = True: .EnableEvents = True End WithEnd Sub
Excel 2007(FileSystemObject – LateBinding)
Sub AdjustMultipleFiles() Dim wbLoopBook As Workbook Dim wsLoopSheet As Worksheet With Application .ScreenUpdating = False: .DisplayAlerts = False: .EnableEvents = False End With With CreateObject("Scripting.FileSystemObject") '// Change path to suit For Each File In .GetFolder("C:\Documents").Files '// ALL Excel 2007 files If .GetExtensionName(File) = "xlsx" Then '// Open Workbook x and Set a Workbook variable to it Set wbLoopBook = Workbooks.Open(Filename:=File.Path, UpdateLinks:=0) '// Loop through all worksheets For Each wsLoopSheet In wbLoopBook.Worksheets '//Update your worksheets here... Next wsLoopSheet '// Close Workbook & Save wbLoopBook.Close SaveChanges:=True '// Release object variable Set wbLoopBook = Nothing End If Next File End With With Application .ScreenUpdating = True: .DisplayAlerts = True: .EnableEvents = True End WithEnd Sub
來源:https://www.icode9.com/content-1-255301.html
聯(lián)系客服