VBA。仅将可见 sheet 导出到单个工作簿

VBA. Export only visible sheet to individual workbook

Sub SaveShtsAsBook() 
    ‘Select all visible and hide sheet’
    Dim Sheet As Worksheet, SheetName$, MyFilePath$, N& 
    MyFilePath$ = ActiveWorkbook.Path & "\" & _ 
    Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4) 
    With Application 
        .ScreenUpdating = False 
        .DisplayAlerts = False 
         '      End With
        On Error Resume Next '<< a folder exists
        MkDir MyFilePath '<< create a folder
        For N = 1 To Sheets.Count 
            Sheets(N).Activate 
            SheetName = ActiveSheet.Name 
            Cells.Copy 
            Workbooks.Add (xlWBATWorksheet) 
            With ActiveWorkbook 
                With .ActiveSheet 
                    .Paste 
                    .Name = SheetName 
                    [A1].Select 
                End With 
                 'save book in this folder
                .SaveAs Filename:=MyFilePath _ 
                & "\" & SheetName & ".xlsx" 
                .Close SaveChanges:=True 
            End With 
            .CutCopyMode = False 
        Next 
    End With 
    Sheet1.Activate 
End Sub 

我有一个工作簿,其中包含许多 sheet,其中有可见的和隐藏的。我只想将每个可见的 sheet 导出到单独的工作簿。上面的当前代码可以对工作簿中的所有 sheet 进行导出,但之后我必须将它们一一删除。希望这能解释我的情况。

所有你需要添加到你的代码来排除隐藏工作表是一个简单的 If..Then statement to check whether the Worksheet.Visible propertyTrueFalse

If Not yourWorsheet.Visible Then... ...然后您跳过该工作表。


以下过程是更简单的总体方法,可以实现您想要完成的...

将可见工作表导出到自己的工作簿:

如果 BeforeAfter 都不是,worksheet.Copy method 将创建一个新工作簿指定。

Sub saveVisibleSheetsAsXLSM()       'saves all visible sheets as new xlsx files
    Const exportPath = "x:\yourDestinationPath\"
    Dim ws As Worksheet, wbNew As Workbook
    For Each ws In ThisWorkbook.Sheets                      'for each worksheet
        If ws.Visible Then                                  'if it's visible:
            Debug.Print "Exporting: " & ws.Name
            ws.Copy '(if no params specified, COPY creates + activates a new wb)
            Set wbNew = Application.ActiveWorkbook          'get new wb object
            wbNew.SaveAs exportPath & ws.Name & ".xlsm", 52 'save new wb
            wbNew.Close                                     'close new wb
            Set wbNew = Nothing                             'cleanup 
        End If
    Next ws
    Set ws = Nothing                                        'clean up 
End Sub

Worksheet.Copy Remarks:

If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet object that contains the copied Worksheet object. The newly created workbook holds the Application.ActiveWorkbook Property (Excel) property and contains a single worksheet. The single worksheet retains the Worksheet.Name Property (Excel) and Worksheet.CodeName Property (Excel) properties of the source worksheet. If the copied worksheet held a worksheet code sheet in a VBA project, that is also carried into the new workbook.

An array selection of multiple worksheets can be copied to a new blank Workbook Object (Excel) object in a similar manner.

(Source: Documentation)