简体   繁体   English

将多个工作簿的 Sheet1 复制到一个新工作簿

[英]Copy Sheet1 of multiple workbooks to one new workbook

I have 99 workbooks in a folder.我在一个文件夹中有 99 个工作簿。 I want to copy sheet1 from each into a new workbook.我想将每个工作表中的 sheet1 复制到一个新工作簿中。 It doesn't matter what order as long as each workbook/sheet1 goes onto a new worksheet in the destination workbook.只要每个工作簿/工作表 1 进入目标工作簿中的新工作表,什么顺序都没有关系。

I have written a code, and tried to sample other codes.我写了一个代码,并试图对其他代码进行采样。 No matter what it will only copy sheet1 of the first 10 workbooks.不管怎样,它只会复制前 10 个工作簿的 sheet1。

How can this work on all the workbooks in the folder?这如何在文件夹中的所有工作簿上工作? My goal is to get the sheets together so I can merge certain cells into a summary sheet.我的目标是将工作表放在一起,以便我可以将某些单元格合并到汇总表中。
I put this code into a module on my destination workbook.我将此代码放入目标工作簿上的模块中。

Sub combineWorkbooks() 
    Path = "C:\Users\james\OneDrive\Desktop\Invoices Jones UK Group\Paid\JJ0001-JJ0099\" 
    fileName = Dir(Path & "*.xls") Do While fileName <> ""
    Workbooks.Open fileName:=Path & fileName, ReadOnly:=True
    For Each sheet In ActiveWorkbook.Sheets
        sheet.Copy After:=ThisWorkbook.Sheets(1)
    Next sheet
    Workbooks(fileName).Close
    fileName = Dir() Loop 
End Sub

在此处输入图片说明

EDIT: this should prevent any issues trying to copy multiple sheets with the same name into the workbook.编辑:这应该可以防止任何尝试将多个具有相同名称的工作表复制到工作簿中的问题。

Sub combineWorkbooks()
    Dim Path, fileName, sheetNum As Long, sheetName As String
    
    Path = "C:\Users\james\OneDrive\Desktop\Invoices Jones UK Group\Paid\JJ0001-JJ0099\"
    fileName = Dir(Path & "*.xls")
    
    Do While fileName <> ""
        With Workbooks.Open(fileName:=Path & fileName, ReadOnly:=True)
            sheetName = .Worksheets(1).Name
            sheetNum = 1
            'if a worksheet with the same name already exists, add
            ' an incrementing number until the name is unique
            If WorksheetExists(sheetName) Then
                Do While WorksheetExists(sheetName & sheetNum)
                    sheetNum = sheetNum + 1
                Loop
                .Worksheets(1).Name = sheetName & sheetNum 'rename if required
            End If
            'copy to end of sheets
            .Worksheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            .Close
        End With
        fileName = Dir()
    Loop
End Sub

Function WorksheetExists(shtName As String, Optional wb As Workbook) As Boolean
    Dim sht As Worksheet

    If wb Is Nothing Then Set wb = ThisWorkbook
    On Error Resume Next
    Set sht = wb.Sheets(shtName)
    On Error GoTo 0
    WorksheetExists = Not sht Is Nothing
End Function

If you're still getting only 10 files then maybe it's an issue with the file names/extensions?如果您仍然只收到 10 个文件,那么可能是文件名/扩展名有问题?

Edit - try listing all of the files:编辑 - 尝试列出所有文件:

Dim Path, fileName
Path = "C:\Users\james\OneDrive\Desktop\Invoices Jones UK Group\Paid\JJ0001-JJ0099\"
fileName = Dir(Path & "*")
Do While fileName <> ""
    Debug.Print fileName
    fileName = Dir()
Loop

What output do you get?你得到什么输出?

It was a file name extension, as you thought.正如您所想,这是一个文件扩展名。

I now have this working.我现在有这个工作。

Sub CombineFiles()
     
    Dim Path            As String
    Dim FileName        As String
    Dim Wkb             As Workbook
    Dim WS              As Worksheet
     
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Path = "C:\Users\james\OneDrive\Desktop\Invoices Jones UK Group\Paid\JJ0800-JJ0899" 'Change as needed
    FileName = Dir(Path & "\*.xlsx", vbNormal)
    Do Until FileName = ""
        Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
        For Each WS In Wkb.Worksheets
            WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        Next WS
        Wkb.Close False
        FileName = Dir()
    Loop
    Application.EnableEvents = True
    Application.ScreenUpdating = True
     
End Sub

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

相关问题 Python - 将工作表从多个工作簿复制到一个工作簿 - Python - Copy sheet from multiple workbooks to one workbook 将多个工作簿的第一张表合并为一个工作簿 - Combine first sheet of multiple workbooks into one workbook 将多个工作簿的行复制到一个主工作簿中 - Copy rows of multiple workbooks into one main workbook 使用VBA或宏将Sheet1从多个工作簿中的数据从特定文件夹导入单个工作簿 - Import Sheet1 Data From Multiple Workbooks From Specific Folder Into Single Workbook Using VBA Or Macros 将复制工作表1更改为在宏中复制工作簿 - Change Copy Sheet1 to Copy Workbook in Macro 将文件夹中所有工作簿的活动工作表复制到新工作簿 - Copy Active sheet of all workbooks in a folder to a new workbook 将多个工作簿(每个工作簿与一张工作表)合并为一个工作簿与多个工作表 - Combine multiple workbooks each with one sheet into one workbook with multiple sheets 从多个工作簿复制单个工作表并粘贴到单个工作簿中 - Copy single sheet from multiple workbooks and paste in a single workbook 将具有多个工作表的工作簿中的数据复制到多个新工作簿中,每个工作表上只有一行 - Copy data from workbook with multiple worksheets into multiple new workbooks with only one row on each worksheet 将工作簿 Sheet1 中的数据复制到主工作表 - Copy Data from Workbooks Sheet1 to Master Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM