一. 方式1
xlOpenXMLWorkbook
:.xlsx 格式的文件xlWorkbookDefault
:当前Excel的格式(当前Excel是什么格式,被拆分出的sheet页所生成的文件就是什么格式)"\"
:可以使用Application.PathSeparator
代替
Sub 拆分工作表( )
Dim sheetObj As Worksheet
Dim MyBook As Workbook
Set MyBook = ActiveWorkbook
For Each sheetObj In MyBook.Sheets
sheetObj.Copy
ActiveWorkbook.SaveAs Filename: = MyBook.Path & "\" & sheetObj.Name , FileFormat: = xlOpenXMLWorkbook
ActiveWorkbook.Close
Next
MsgBox "文件已经被拆分完毕!"
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 Worksheet
Dim strPath As String
Dim visibilityStatus As Integer
With Application.FileDialog( msoFileDialogFolderPicker)
If .Show Then
strPath = .SelectedItems( 1 )
Else
Exit Sub
End If
End With
If Right ( strPath, 1 ) <> Application.PathSeparator Then
strPath = strPath & Application.PathSeparator
End If
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For Each sht In Worksheets
visibilityStatus = sht.Visible
If visibilityStatus <> xlSheetHidden And visibilityStatus <> xlSheetVeryHidden Then
sht.Copy
With ActiveWorkbook
.SaveAs strPath & sht.Name , xlWorkbookDefault
.Close True
End With
End If
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "处理完成。" , , "提醒"
End Sub