将一个工作簿拆分为多个工作表

将一个工作簿拆分为多个工作表

1.打开要拆分的工作簿-【开发工具】-【宏】

image-20210511211026339

2.输入名称,点击创建。

image-20210511211115379

3.输入以下代码。

Sub 拆分工作表()

    On Error Resume Next

    Application.DisplayAlerts = False

    Application.ScreenUpdating = False

    Dim Sht As Worksheet, Nwb As Workbook, Owb As Workbook

    Dim OPath As String, NPath As String

    Set Owb = ActiveWorkbook

    OPath = Owb.Path

      If Len(OPath) <> 0 Then

         NPath = OPath & "" & "\" & Split(Owb.Name, ".x")(0) & "-拆分"

         MkDir NPath

            For Each Sht In Owb.Worksheets

                Sht.Copy

                Set Nwb = ActiveWorkbook

                Nwb.SaveAs NPath & "" & "\" & Split(Owb.Name, ".x")(0) & "-" & Sht.Name & ".xlsx"

                Nwb.Close

            Next

            MsgBox "拆分后的文件已保存至:" & NPath

       End If

    Application.DisplayAlerts = True

    Application.ScreenUpdating = True

End Sub

4.返回到工作表中,点击宏,选择执行即可。

image-20210511211334098

posted @ 2021-05-11 21:16  秋弦  阅读(1149)  评论(0编辑  收藏  举报