简体   繁体   English

MS Excel-用于将多个工作表中的值合并为单个工作表的宏

[英]MS Excel - Macros for consolidating values from multiple sheets into a single sheet

Consider i have 4 workbooks with the following structure... 考虑我有以下结构的4个工作簿...

1. Main.xlsx
    Name    Jan   Feb  Mar
       A
       B
       C

2. Jan.xlsx       
     Name     Jan
      A       3.3
      B       6.4
      C       5.3

3. Feb.xlsx       
     Name     Feb
      A       1.3
      B       3.4
      C       5.5

4. Mar.xlsx       
     Name     Mar
      A       1.3
      B       3.4
      C       5.5

I need to combine them like 我需要像把它们结合起来

1. Main.xlsx
        Name    Jan   Feb  Mar
           A    3.3   1.3  1.3
           B    6.4   3.4  3.4
           C    5.3   5.5  5.5

And i need to automate the process... 而且我需要使过程自动化...

And i guess i can do this with macros...? 我想我可以用宏来做到这一点...? Can anyone suggest some way with which i can proceed with the macro? 谁能建议我可以继续使用宏的某种方式?

Thanks for your time.... 谢谢你的时间....

You can use ADO. 您可以使用ADO。 Here are some notes. 这里有一些注意事项。

''Must use macro-enabled file type, eg .xlsm
''The code was run from Main.xlsm, but should work in any 
''Excel file.
Dim fs As Object
Dim rs As Object
Dim cn As Object
Dim strSQL As String
Dim strCon As String
Dim i, f, s, m, ml
Dim aFiles As Variant

''For looking up files, Dir would work, too
Set fs = CreateObject("Scripting.FileSystemObject")

''Array for file names and month names
''Space for months up to one less than the current month
ReDim aFiles(Month(Date) - 2, 1)

''Fill the array ...
For i = 1 To Month(Date) - 1

    ''With files called mmm.xlsx ...
    m = Format(CDate("2010/" & i & "/1"), "mmm")
    ''Found in C:\Docs
    f = "C:\Docs\" & m & ".xlsx"

    ''Checking first that the file exists
    If fs.FileExists(f) Then
        aFiles(i - 1, 0) = f
        aFiles(i - 1, 1) = m
    Else
        Debug.Print "Missing : " & f
    End If
Next

''Build the SQL string ...
For i = 1 To UBound(aFiles, 1)
    ''For joins, brackets = number of months -1
    strSQL = strSQL & "("
Next

''Using Main.xlsm subquery as the basis for all Names ...
strSQL = strSQL & "(SELECT [Name] FROM [Sheet1$] IN '' " _
   & "[Excel 8.0;database=C:\docs\Main.xlsm]) As Main LEFT JOIN "

''Left Join to all found files as subqueries aliased as mmm name ...
For i = 0 To UBound(aFiles, 1)
    strSQL = strSQL & "(SELECT [Name]," & aFiles(i, 1) _
         & " FROM [Sheet1$] IN '' [Excel 8.0;database=" _
    & aFiles(i, 0) & "]) AS " & aFiles(i, 1) & " ON Main.Name = " & aFiles(i, 1) 
         & ".Name) LEFT JOIN "
Next

''Remove final Left Join and bracket ...
strSQL = Left(strSQL, Len(strSQL) - 12)

''Get a list of months ...
For i = 0 To UBound(aFiles, 1)
    ml = ml & "," & aFiles(i, 1)
Next

''Add the outer query, and that is the SQL string finished.
strSQL = "SELECT Main.Name," & Mid(ml, 2) & " FROM " & strSQL

''This uses main.xlsm in the connection string, but it is
''not important which file is used because the SQL string
''is build using IN (keyword) to get the various files
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
    & Workbooks("main.xlsm").FullName _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

''Connection and recordset objects
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

rs.Open strSQL, cn

''Fill heading into Sheet2
For i = 0 To rs.Fields.Count - 1
    Sheets("Sheet2").Cells(1, i + 1) = rs.Fields(i).Name
Next

''Fill data into Sheet2
Sheets("Sheet2").Cells(2, 1).CopyFromRecordset rs

暂无
暂无

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

相关问题 Excel 宏将数据从主表填充到多个模板表中 - Excel Macros to populate data from master sheet into multiple template sheets Excel 2007-将来自多个工作表的非连续数据合并到一个主工作表(即预算工作簿)中 - Excel 2007 - Consolidating non contiguous data from multiple sheets into a master sheet (ie a budget workbook) 将多个工作表中的单行数据合并为一个 - 循环宏? - Consolidating single rows of data from multiple sheets into one - looping the macro? Excel VBA:将单元格从多个工作表复制到单个工作表 - Excel VBA: Copy cells from multiple sheets to a single sheet 使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要 - Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python 将 excel 工作表从文件夹合并到一个工作表中 - Merge the excel sheets from a folder into a single sheet 如何读取包含多个工作表的Excel工作表,以及仅编辑一个工作表 - How to read an Excel sheet with multiple sheets, and edit only a single sheet 如何使用 Excel 或 Google 工作表从单独工作表中的多个工作表的特定列获取唯一值? - How to get unique values from a specific column from multiple sheets in a separate sheet using Excel or Google sheets? 从多个Excel工作表中复制数据,然后使用VBScript将其追加到单个Excel工作表中 - Copy data from multiple excel sheets and append that to a single excel sheet using VBScript 如何将多个Excel工作表合并到带有标签的单个Excel工作表中? - How to combine multiple excel sheets into single excel sheet with tabs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM