excel 创建另一个文件,但不将工作表拆分为单独的文件

jogvjijk  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(129)

将工作表分隔为单独的文件
嗨我正在使用代码

Sub Split_Sheet_into_ExcelFiles()
    Dim FilePath As String
    FilePath = Application.ActiveWorkbook.Path
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each Sheet In ThisWorkbook.Sheets
    Sheet.Copy
    Application.ActiveWorkbook.SaveAs Filename:=FilePath & "\" & Sheet.Name & ".xlsx"
    Application.ActiveWorkbook.Close False
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

它创建了相同的文件,但我试图将多个工作表拆分成单独的文件。知道我输入错误吗?谢谢

bfhwhh0e

bfhwhh0e1#

将每张图纸导出到单个文件

Option Explicit

Sub ExportSheets()
    
    Const PROC_TITLE As String = "Export Sheets"
    
    Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
    
    Dim dFilePath As String: dFilePath = swb.Path
    
    If Len(dFilePath) = 0 Then
        MsgBox "The path cannot be determined." & vbLf & "Please save the " _
            & "workbook before using this procedure.", vbCritical, PROC_TITLE
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    Dim dwb As Workbook, ssh As Object, sshCount As Long
    
    For Each ssh In swb.Sheets
        If ssh.Visible = xlSheetVisible Then ' sheet is visible
            ssh.Copy
            Set dwb = Workbooks(Workbooks.Count)
            Application.DisplayAlerts = False ' overwrite without confirmation
                dwb.SaveAs Filename:=dFilePath & "\" & ssh.Name
            Application.DisplayAlerts = False
            dwb.Close SaveChanges:=False
            sshCount = sshCount + 1
        'Else ' sheet is not visible; do nothing!?
        End If
    Next ssh

    Application.ScreenUpdating = True
    
    MsgBox sshCount & " sheet" & IIf(sshCount = 1, "", "s") _
        & " exported.", vbInformation, PROC_TITLE

End Sub

相关问题