简体   繁体   English

Excel VBA 将指定的一组工作表复制到新工作簿/从副本中排除工作表

[英]Excel VBA copying specified set of worksheets to new workbook/excluding sheet from copy

I am trying to copy only data from one workbook into a new one, but with only four of the existing worksheets.我试图仅将一个工作簿中的数据复制到一个新工作簿中,但只有四个现有工作表。 The code below allows me to successfully copy all worksheets to a new workbook.下面的代码允许我成功地将所有工作表复制到新工作簿。 This worked fine before, but now I only want to copy sheet 2-7, thus excluding sheet 1.这以前工作得很好,但现在我只想复制第 2-7 页,因此不包括第 1 页。

This is done by a user copying data into sheet 1 and the data will be populated to sheets 2-5.这是通过用户将数据复制到工作表 1 中来完成的,数据将填充到工作表 2-5 中。 Sheet 6 & 7 contains metadata which will be the same for all new workbooks.工作表 6 和 7 包含对所有新工作簿都相同的元数据。 To be able to import the copied data, I need a new workbook with sheets 2-7.为了能够导入复制的数据,我需要一个包含 2-7 页的新工作簿。

Sub Button1_Click()

Dim Output As Workbook
Dim Current As String
Dim FileName As String


Set Output = ThisWorkbook
Current = ThisWorkbook.FullName

Application.DisplayAlerts = False

Dim SH As Worksheet
For Each SH In Output.Worksheets
    SH.UsedRange.Copy
    SH.UsedRange.PasteSpecial xlPasteValuesAndNumberFormats, _
        Operation:=xlNone, SkipBlanks:=True, Transpose:=False
Next
    
FileName = ThisWorkbook.Path & "\" & "Generic name.xlsx" 'Change name as needed
Output.SaveAs FileName, XlFileFormat.xlOpenXMLWorkbook
Workbooks.Open Current
Output.Close
Application.DisplayAlerts = True

End Sub

Any suggestions on how improve the code to only copy specified sheets, or to exclude sheet 1?关于如何改进代码以仅复制指定的工作表或排除工作表 1 的任何建议?

Add an IF statement after the For each loop to exclude Sheet1在 For each 循环后添加 IF 语句以排除 Sheet1

For Each SH In Output.Worksheets
If SH.Name <> "Sheet1" Then
    SH.UsedRange.Copy
    SH.UsedRange.PasteSpecial xlPasteValuesAndNumberFormats, _
    Operation:=xlNone, SkipBlanks:=True, Transpose:=False
End If
Next

Copy a Set of Worksheets to Another Workbook将一组工作表复制到另一个工作簿

Option Explicit

Sub Button1_Click()
    
    ' Constants
    
    Const dFileName As String = "Generic name.xlsx"
    Dim DoNotCopy As Variant: DoNotCopy = Array(1) ' add more: Array(1, 7, 8)
    Const ConversionWorksheetsCount As Long = 4
    
    ' Write the names of the desired worksheets to an array.
    
    Dim swb As Workbook: Set swb = ThisWorkbook
    Dim swsCount As Long: swsCount = swb.Worksheets.Count
    Dim dwsNames() As String: ReDim dwsNames(1 To swsCount)
    
    Dim sws As Worksheet
    Dim sCount As Long
    Dim dCount As Long
    
    For Each sws In swb.Worksheets
        sCount = sCount + 1
        If IsError(Application.Match(sCount, DoNotCopy, 0)) Then
            dCount = dCount + 1
            dwsNames(dCount) = sws.Name
        ' Else ' worksheet index found in the 'DoNotCopy' array.
        End If
    Next sws
    If dCount = 0 Then
        MsgBox "No worksheets found.", vbCritical
        Exit Sub
    End If
    
    If dCount < swsCount Then
        ReDim Preserve dwsNames(1 To dCount)
    End If
    
    Application.ScreenUpdating = False
    
    ' Copy the desired worksheets to a new (destination) workbook.
    
    swb.Worksheets(dwsNames).Copy
    Dim dwb As Workbook: Set dwb = ActiveWorkbook
    
    ' Do the conversions.
    
    Dim dws As Worksheet
    Dim n As Long
    
    For n = 1 To ConversionWorksheetsCount
        On Error Resume Next
            Set dws = dwb.Worksheets(n)
        On Error GoTo 0
        If Not dws Is Nothing Then ' destination worksheet exists
            dws.Activate ' needed for '.Cells(1).Select'
            With dws.UsedRange
                .Copy
                .PasteSpecial xlPasteValuesAndNumberFormats, _
                    Operation:=xlNone, SkipBlanks:=True, Transpose:=False
                .Cells(1).Select ' cosmetics
            End With
        'Else ' destination worksheet doesn't exist
        End If
    Next n
    'dwb.Worksheets(1).Activate ' cosmetics        

    ' Save the new (destination) workbook.
    
    Dim dFilePath As String: dFilePath = swb.Path & "\" & dFileName
    Application.DisplayAlerts = False ' overwrite without confirmation
    dwb.SaveAs dFilePath, xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    dwb.Close
    
    ' Note that you never modified the source. It's in the same state as before.
    
    Application.ScreenUpdating = True
    
    MsgBox "Workbook created.", vbInformation
    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅将可见工作表中的可见单元格复制到新工作簿中,即Excel 2007 VBA - Copying only the visible cells from visible worksheets into a new workbook, excel 2007 VBA Excel VBA-将所有工作表从特定工作簿复制到活动工作簿中 - Excel VBA - Copying all Worksheets from a specific workbook into an active workbok VBA 将工作表复制到工作簿末尾(带有隐藏工作表) - VBA Copy Sheet to End of Workbook (with Hidden Worksheets) 将数据从工作簿中的多个工作表复制到单独工作簿中的不同工作表-VBA Excel - Copying Data from Multiple Worksheets in Workbooks to Differing Worksheets in Separate Workbook - VBA Excel EXCEL VBA:将工作表中的工作表复制到其他位置的工作簿中 - EXCEL VBA: Copy Sheet from a workbook to another workbook in different location 将具有特定名称的Excel工作表从多个工作簿复制到新工作簿 - Copy Excel Worksheets with Specific Name from Multiple Workbooks to New Workbook Excel将活动工作表和指定工作表复制到新工作簿 - Excel Copy active sheet and specified sheets to new workbook EXCEL复制到新工作簿时设置工作表顺序 - EXCEL Set sheet order when copying to a new workbook Excel VBA将内容从一个工作表复制到另一个工作簿工作表 - Excel VBA copy content from one Sheet into other Workbook sheet Excel VBA-将工作表复制到新工作簿X次 - Excel VBA - Copy Sheet to new workbook X times
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM