一. 方式1
 
- xlOpenXMLWorkbook:.xlsx格式的文件
- xlWorkbookDefault:当前Excel的格式(当前Excel是什么格式,被拆分出的sheet页所生成的文件就是什么格式)
- "\":可以使用- Application.PathSeparator代替
Sub 拆分工作表()Dim sheetObj As WorksheetDim MyBook As WorkbookSet MyBook = ActiveWorkbookFor Each sheetObj In MyBook.SheetssheetObj.CopyActiveWorkbook.SaveAs Filename:=MyBook.Path & "\" & sheetObj.Name, FileFormat:=xlOpenXMLWorkbookActiveWorkbook.CloseNextMsgBox "文件已经被拆分完毕!"End Sub
 
 
二. 方式2
 
- sht.Visible:判断sheet页是否可见- -1:xlSheetVisible(工作表可见)
- 0:xlSheetHidden(工作表隐藏,但可以通过工作表选项卡显示)
- 2:xlSheetVeryHidden(工作表隐藏,并且在工作表选项卡中不可见)
 
- With Application.FileDialog(msoFileDialogFolderPicker):用于显示文件夹选择对话框的语句- msoFileDialogOpen:打开文件对话框,用于选择要打开的文件。
- msoFileDialogSaveAs:另存为文件对话框,用于选择保存文件的路径和文件名。
- msoFileDialogFilePicker:文件选择对话框,允许选择一个或多个文件。
- msoFileDialogFolderPicker:文件夹选择对话框,用于选择文件夹路径。
 
- Right(strPath, 1):获取右侧第一个字符
- VBA中的For Each没有类似于contiune的语法,只能使用IF Else来处理跳过
Sub EachShtToWorkbook()Dim sht As WorksheetDim strPath As StringDim visibilityStatus As IntegerWith Application.FileDialog(msoFileDialogFolderPicker)If .Show ThenstrPath = .SelectedItems(1)ElseExit SubEnd IfEnd WithIf Right(strPath, 1) <> Application.PathSeparator ThenstrPath = strPath & Application.PathSeparatorEnd IfApplication.DisplayAlerts = FalseApplication.ScreenUpdating = FalseFor Each sht In WorksheetsvisibilityStatus = sht.VisibleIf visibilityStatus <> xlSheetHidden And visibilityStatus <> xlSheetVeryHidden Thensht.CopyWith ActiveWorkbook.SaveAs strPath & sht.Name, xlWorkbookDefault.Close TrueEnd WithEnd IfNextApplication.ScreenUpdating = TrueApplication.DisplayAlerts = TrueMsgBox "处理完成。", , "提醒"End Sub