![](/img/trans.png)
[英]Excel VBA - Run Macro / Open another file / Run that files macro / Save & Close
[英]Excel VBA Macro to Open, Protect, Save and Close Multiple Files in a Folder
那里。
我需要運行一個 Excel VBA 宏,它將在單個文件夾中打開 50 個 .xlsx 文件,我想,保護工作表,保存並關閉。
我會喜歡一個對話框,它告訴我找到了多少文件來首先確認文件夾中的文件數。
這是建議我打開、保護、保存和關閉單個文件的代碼(但是,我再次希望為 50 個左右的多個文件執行此操作)。
Sub Macro1()
'
' Macro1 Macro
'
'
ChDir "G:\Folder\Subfolder\Projects"
Workbooks.Open Filename:= _
"G:\Folder\Subfolder\Projects\Filename.xlsx"
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True
ActiveWorkbook.Save
ActiveWindow.Close
End Sub
這可以修改嗎,或者我需要新代碼嗎? 有人能幫我嗎?
謝謝!
這應該可以解決問題。
Sub protect_excel_files_sheets_in_folder()
Dim wb As Workbook
Dim sheet As Worksheet
Dim file_path As String, work_file As String, file_types As String
Dim work_folder As String
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.ButtonName = "Select"
.Show
If .SelectedItems.Count <> 1 Then
Exit Sub
End If
work_folder = .SelectedItems(1) & "\"
End With
file_types = "*.xls*"
work_file = Dir(work_folder & file_types)
Do While work_file <> ""
Set wb = Workbooks.Open(Filename:=work_folder & work_file)
For Each sheet In wb.Sheets sheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingCells:=True
Next
Sheets(0).Activate
wb.Close SaveChanges:=True
work_file = Dir
Loop
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
CleanExit:
End Sub
我碰巧有類似的東西,在我的圖書館中密碼保護和取消保護所有文件和工作表。 我取出了密碼部分,它應該對你有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.