简体   繁体   English

将多个工作簿中的多个工作表合并到一个具有相同工作表的工作簿中,但是将合并多个工作表中的数据

[英]Consolidate multiple sheets in multiple workbooks into one workbook with the same sheets but the data in the multiple sheets will be consolidated

I tried looking for this across the web but for my purpose, I have been unable so far to optimize the code required. 我尝试在整个网络上查找此文件,但出于我的目的,到目前为止,我无法优化所需的代码。 This is what I am trying to accomplish: 这是我要完成的工作:

I have files called Excel 1, Excel 2, Excel 3 and Master Excel. 我有名为Excel 1,Excel 2,Excel 3和Master Excel的文件。 All of the files have the same number of worksheets, worksheet name and the same structure when it comes to the header and such. 在标题等方面,所有文件都具有相同数量的工作表,工作表名称和相同的结构。

I am trying to consolidate the values of Excel 1, Excel 2 and Excel 3 to the Master file. 我正在尝试将Excel 1,Excel 2和Excel 3的值合并到Master文件中。

So on the Master File, if there is sheet named 1000, then copy paste a range from Excel 1 sheet named 1000. Then look for sheet 1000, in Excel 2 and copy paste a range on the blank line following the last row used on Master file Sheet 1000. 因此,在主文件上,如果有名为1000的工作表,则从Excel 1名为1000的工作表中复制粘贴一个范围。然后在Excel 2中查找工作表1000,并在主文件使用的最后一行之后的空白行上复制并粘贴一个范围文件工作表1000。

The range is always the row after the header (this is fixed on all sheets) till the last row with data on a specific column. 范围始终是标题之后的行(在所有工作表上都固定),直到最后一行包含特定列上的数据。

Now there are multiple sheets in each workbooks and all the worksheets will have the same name. 现在,每个工作簿中都有多个工作表,并且所有工作表将具有相同的名称。

Also the filepath of the files will be constant so I dont want an option to choose from. 文件的文件路径也将是恒定的,所以我不希望从中选择一个选项。

The below code is able to loop through the worksheets and I can also define the copy paste range perfectly but only issue with the below is that I dont know how to match a target sheet with a destination sheet meaning sheet 1000's data in excel 1 file to be pasted to sheet 1000 in the master file. 下面的代码能够遍历工作表,并且我也可以完美地定义复制粘贴范围,但是下面的唯一问题是我不知道如何将目标工作表与目标工作表匹配,这意味着工作表1000在excel 1文件中的数据粘贴到主文件中的工作表1000中。

Sub test()

Dim MyFile As String, MyFiles As String, FilePath As String
Dim erow As Long
'~~> Put additional variable declaration
Dim wbMaster As Workbook, wbTemp As Workbook
Dim wsMaster As Worksheet, wsTemp As Worksheet

FilePath = "\\AM01PCIFS01.global.root\HomeDirs$\yameen.sarwar\Desktop\Test\II1\"
MyFiles = "\\AM01PCIFS01.global.root\HomeDirs$\yameen.sarwar\Desktop\Test\II1\*.xlsx"
MyFile = Dir(MyFiles)

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

'~~> Set your declared variables
Set wbMaster = ThisWorkbook 'if you want to consolidate files in this workbook
Set wsMaster = wbMaster.Sheets("Sheet1") 'replace Sheet1 to suit

Do While Len(MyFile) > 0
    'Debug.Print MyFile
    If MyFile <> "master.xlsm" Then
        '~~> Open the file and at the same time, set your variable
        Set wbTemp = Workbooks.Open(Filename:=FilePath & MyFile, ReadOnly:=True)
        Set wsTemp = wbTemp.Sheets(1) 'I used index, you said there is only 1 sheet
        '~~> Now directly work on your object
        With wsMaster
            erow = .Range("A" & .Rows.Count).End(xlUp).Row 'get the last row
            '~~> Copy from the file you opened
            wsTemp.Range("A2:S20").Copy 'you said this is fixed as well
            '~~> Paste on your master sheet
            .Range("A" & erow).Offset(1, 0).PasteSpecial xlPasteValues
        End With
        '~~> Close the opened file
        wbTemp.Close False 'set to false, because we opened it as read-only
        Set wsTemp = Nothing
        Set wbTemp = Nothing
    End If
    '~~> Load the new file
    MyFile = Dir
Loop

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

To take sheetnames in wbMaster and reference a sheet with the same name in wbTemp, you can pass the name through a variable. 要在wbMaster中获取工作表名称并在wbTemp中引用具有相同名称的工作表,可以通过变量传递名称。 Here is a couple of lines that will loop through your sheets in wbMaster 这几行代码将循环遍历wbMaster中的工作表

Dim strSheetname as String

For i = 1 To wbMaster.Sheets.Count
      strSheetName = wbMaster.Sheets(i).Name
      Set wsTemp = wbTemp.Sheets(strSheetName)
      'Do whatever you need here with wsTemp
Next i

This code lacks error handling (ie if a sheet exists in wbMaster that does not exist in wbTemp, you will get an out of range error) but this will get you started. 此代码缺少错误处理(即,如果wbMaster中存在工作表,而wbTemp中不存在工作表,则将出现超出范围的错误),但这将使您入门。

Try this (see my comments in the code), but I made some small alterations in your Do While loop 尝试此操作(请参阅代码中的注释),但是我在您的“ Do While”循环中做了一些小改动

Sub test()

Dim MyFile As String, MyFiles As String, FilePath As String
Dim erow As Long
'~~> Put additional variable declaration
Dim wbMaster As Workbook, wbTemp As Workbook
Dim wsMaster As Worksheet, wsTemp As Worksheet
Dim i As Integer

FilePath = "\\AM01PCIFS01.global.root\HomeDirs$\yameen.sarwar\Desktop\Test\II1\"
MyFiles = "\\AM01PCIFS01.global.root\HomeDirs$\yameen.sarwar\Desktop\Test\II1\*.xlsx"
MyFile = Dir(MyFiles)

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

'~~> Set your declared variables
Set wbMaster = ThisWorkbook 'if you want to consolidate files in this workbook

Do While Len(MyFile) > 0
    'Debug.Print MyFile
    If MyFile <> "master.xlsm" Then
        '~~> Open the file and at the same time, set your variable
        Set wbTemp = Workbooks.Open(Filename:=FilePath & MyFile, ReadOnly:=True)
        'Start the loop of sheets within the source workbook
        For i = 1 To wbTemp.Sheets.Count
            Set wsTemp = wbTemp.Sheets(i) 'I used index, you said there is only 1 sheet
            '~~> Now directly work on your object
            With wbMaster.Worksheets(wsTemp.Name) 'This matches the sheet name in the source workbook to the sheet name in the target workbook
                erow = .Range("A" & .Rows.Count).End(xlUp).Row 'get the last row of target sheet
                '~~> Copy from the file you opened
                wsTemp.Range("A2:S20").Copy 'you said this is fixed as well
                '~~> Paste on your master sheet
                .Range("A" & erow).Offset(1, 0).PasteSpecial xlPasteValues
                Application.CutCopyMode = False
            End With
        Next i
        '~~> Close the opened file
        wbTemp.Close False 'set to false, because we opened it as read-only
    End If
    '~~> Load the new file
    MyFile = Dir
Loop

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM