VBA选取特定工作表并批量导出为单独文件
时间: 2024-05-06 20:21:44 浏览: 127
您可以使用以下 VBA 代码选取特定工作表并批量导出为单独文件:
```
Sub ExportWorksheets()
Dim xWorksheet As Worksheet
Dim xWorkbook As Workbook
Dim xFolderPath As String
Dim xDialog As FileDialog
Set xDialog = Application.FileDialog(msoFileDialogFolderPicker)
xDialog.AllowMultiSelect = False
xDialog.Title = "Select a folder"
If xDialog.Show = -1 Then
xFolderPath = xDialog.SelectedItems(1)
End If
Set xWorkbook = Application.ActiveWorkbook
For Each xWorksheet In xWorkbook.Worksheets
xWorksheet.Select
xWorksheet.Copy
xWorkbook.SaveAs Filename:=xFolderPath & "\" & xWorksheet.Name & ".xlsx", FileFormat:=51
xWorkbook.Close False
Set xWorkbook = Application.Workbooks.Open(ThisWorkbook.FullName)
Next
End Sub
```
您可以将上面的代码复制到 VBA 编辑器中,并按 F5 键运行它。然后会打开一个文件夹选择器,让您选择保存导出文件的文件夹。接着,它将逐一选中每个工作表、复制它、保存为单独的文件并关闭工作簿。最后,它将重新打开工作簿,以便继续处理下一个工作表。导出的每个文件都将以其工作表名称命名,并保存为 xlsx 格式。
阅读全文