简体   繁体   English

如何在一个工作簿中从多个工作簿中移动或复制以相同名称开头的工作表

[英]How to move or copy sheets starting with same name from multiple workbooks in one a single workbook

I have 4 excel workbooks each containing 35 sheets.我有 4 个 excel 工作簿,每个工作簿包含 35 张。 Each workbook have one sheet where, the first there character of the sheet name is same in all the workbooks.每个工作簿都有一个工作表,其中工作表名称的第一个字符在所有工作簿中都相同。 For example: tes_8,tes_9,tes_3,tes_2 are sheet names in workbook1,workbook2,workbook3 and workbook4 respectively.例如: tes_8,tes_9,tes_3,tes_2 分别是 workbook1、workbook2、workbook3 和 workbook4 中的工作表名称。

Now I want to copy sheets with sheet name having the first three character same from these four workbooks into a single workbook, so here I want a new excel workbook containing these four sheets: tes_8,tes_9 tes_3,tes_2现在我想将这四个工作簿中前三个字符相同的工作表名称复制到一个工作簿中,所以在这里我想要一个包含这四个工作表的新 excel 工作簿:tes_8,tes_9 tes_3,tes_2

I was attempting to do this manually ie by right clicking on the sheet then,select move or copy option then,check the create a copy checkbox and then select the workbook you want your sheet to move to.我试图手动执行此操作,即通过右键单击工作表然后,select 移动或复制选项,然后选中创建副本复选框,然后选中 select 您希望工作表移动到的工作簿。 Since there are 35 sheets moving manually is taking a lot of time.由于手动移动 35 张纸需要很多时间。

You can try the below example code:您可以尝试以下示例代码:

Set closedBook = Workbooks.Open("Destination workbook location") Set closedBook = Workbooks.Open("目标工作簿位置")

For i = 1 To Worksheets.Count对于 i = 1 到 Worksheets.Count

select case left(Worksheets(i).Name,5) '' As your sheet name is 5 letters (tes_8,tes_9) select case left(Worksheets(i).Name,5) '' 因为您的工作表名称是 5 个字母 (tes_8,tes_9)

case tes_8,tes_9 tes_3,tes_2 '' checking whether it is in the start of sheet names case tes_8,tes_9 tes_3,tes_2 '' 检查是否在工作表名称的开头

worksheets(i).Copy Before:=closedBook.Sheets(1) '' copy those sheets to destination workbook worksheets(i).Copy Before:=closedBook.Sheets(1) '' 将这些工作表复制到目标工作簿

closedBook.Close SaveChanges:=True closedBook.Close SaveChanges:=True

end select结束 select

Next i接下来我

I'm new in this community.我是这个社区的新手。 I hope you can use it to create a vba program.我希望你可以用它来创建一个 vba 程序。

Copy Worksheets That Start With... to New Workbook将以...开头的工作表复制到新工作簿

  • In all (source) workbooks from the list (adjust), it will locate the first worksheet whose name starts with tes_ (adjust) and copy it to a new (destination) workbook.在列表 (adjust) 中的所有(源)工作簿中,它将找到名称以tes_开头的第一个工作表(adjust)并将其复制到新的(目标)工作簿中。
  • If a source workbook is open it will leave it open, if not, using the path C:\Test (adjust), it will open it and close it after copying the worksheet.如果源工作簿打开,它将保持打开状态,如果没有,使用路径C:\Test (调整),它将在复制工作表后打开并关闭它。
Option Explicit

Sub CopyWorksheets()
    
    ' Define constants.
    Const swbNamesList As String = "wb1.xlsx,wb2.xlsx,wb3.xlsx,wb4.xlsx"
    Const sFolderPath As String = "C:\Test"
    Const swsNameLeft As String = "tes_"
    
    ' Determine and validate the source path ('sPath').
    Dim sPath As String: sPath = Dir(sFolderPath, vbDirectory)
    If Len(sPath) = 0 Then
        MsgBox "The path '" & sFolderPath & "' was not found.", vbCritical
        Exit Sub
    End If
    sPath = sFolderPath
    If Right(sPath, 1) <> Application.PathSeparator Then
        sPath = sPath & Application.PathSeparator
    End If
    
    ' Write the source workbook names from the list to an array ('swbNames').
    Dim swbNames() As String: swbNames = Split(swbNamesList, ",")
    
    Application.ScreenUpdating = False
    
    ' Declare variables used for the first time
    ' in the following For...Next loop.
    Dim swb As Workbook ' Current Source Workbook
    Dim sws As Worksheet ' Current Source Worksheet
    Dim swbPath As String ' Current Source Path
    Dim swbWasClosed As Boolean ' Closed Boolean
    Dim dwb As Workbook ' Destination Workbook
    Dim dwsCount As Long ' Destination Worksheets Count
    Dim n As Long ' Source Workbook Names Counter
    
    ' Loop through the elements of the array.
    For n = 0 To UBound(swbNames)
        ' Attempt to reference the source workbook.
        On Error Resume Next
            Set swb = Workbooks(swbNames(n))
        On Error GoTo 0
        If swb Is Nothing Then ' the source workbook is not open
            ' Attempt to open the source workbook.
            swbPath = sPath & swbNames(n)
            On Error Resume Next
                Set swb = Workbooks.Open(swbPath)
            On Error GoTo 0
            swbWasClosed = True
        'Else ' the source workbook is open
        End If
        If Not swb Is Nothing Then ' the source workbook is open
            For Each sws In swb.Worksheets
                If InStr(1, sws.Name, swsNameLeft, vbTextCompare) = 1 Then
                    If dwsCount = 0 Then
                        sws.Copy ' creates a new single-worksheet workbook
                        Set dwb = Workbooks(Workbooks.Count) ' reference it
                    Else
                        sws.Copy After:=dwb.Sheets(dwb.Sheets.Count)
                    End If
                    dwsCount = dwsCount + 1
                    Exit For ' stop looping because the worksheet was found
                'Else ' not a match; do nothing
                End If
            Next sws
            If swbWasClosed Then ' the source workbook was closed
                swb.Close SaveChanges:=False
                swbWasClosed = False ' reset the variable
            'Else ' the source workbook was open, let it be; do nothing
            End If
            Set swb = Nothing ' reset the variable
        'Else ' the source file (workbook) doesn't exist; do nothing
        End If
    Next n

    If dwsCount > 0 Then dwb.Saved = True ' just for easy closing while testing

    Application.ScreenUpdating = True
    
    ' Inform.
    Select Case dwsCount
    Case 0
        MsgBox "No worksheets found.", vbCritical
    Case 1
        MsgBox "Only one worksheet found.", vbExclamation
    Case n
        MsgBox "All " & n & " worksheets found.", vbInformation
    Case Else
        MsgBox "Only " & dwsCount & " worksheets found.", vbExclamation
    End Select

End Sub

暂无
暂无

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

相关问题 将多个工作簿中的所有工作表复制到单个工作簿 - Copy all sheets from multiple workbooks to a single workbook 将文件夹中的不同工作簿复制到一个工作簿中的不同工作表中 - Copy different workbooks from an folder into different sheets in one workbook 如何仅将文件夹中的第一批工作簿复制到一个Excel工作簿中 - How to copy only the first sheets of workbooks in a folder into one excel workbook 从多个工作簿复制单个工作表并粘贴到单个工作簿中 - Copy single sheet from multiple workbooks and paste in a single workbook Excel VBA将多个工作簿中的多个工作表合并到一个工作簿中 - excel vba merge multiple sheets from multiple workbooks into one workbook 将多个工作簿中的多个工作表合并到一个具有相同工作表的工作簿中,但是将合并多个工作表中的数据 - Consolidate multiple sheets in multiple workbooks into one workbook with the same sheets but the data in the multiple sheets will be consolidated 将特定选项卡从多个工作簿移动到单个工作簿 - Move specific tab from multiple workbooks into a single workbook 从多个工作簿复制并粘贴到单个工作簿中,再到下一个空白行 - Copy and Paste from multiple workbooks into a single workbook into the next blank row 将多行从多个工作簿复制到一个主工作簿 - Copy multiple rows from multiple workbooks to one master workbook Excel VBA。 从多个工作簿复制数据并粘贴到一个工作簿的同一工作表中 - Excel VBA. Copy data from multiple workbooks and paste in one workbook same worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM