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

发布于 2024-08-31 02:53:13 字数 665 浏览 10 评论 0原文

考虑我有 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

我需要将它们组合起来

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

并且我需要自动化该过程...

我想我可以用宏来做到这一点...?谁能建议我可以继续使用宏的某种方法?

谢谢你的时间....

Consider i have 4 workbooks with the following structure...

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....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-09-07 02:53:13

您可以使用 ADO。这里有一些注意事项。

''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

You can use 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文