![](/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.