简体   繁体   English

如何在 VBA 中使用 SQL 将两个工作簿与一个公共列合并

[英]How to merge two workbooks with one common column using SQL in VBA

I believe i can do this through Macro as well but i would like to get introduced to ADODB and SQL queries in VBA.我相信我也可以通过宏来做到这一点,但我想在 VBA 中介绍 ADODB 和 SQL 查询。 The datasets are not in Access but separate workbooks and the SQL query for the two workbooks are as follows:数据集不在 Access 中,而是在单独的工作簿中,两个工作簿的 SQL 查询如下:

SELECT getRollpHierRpt.Field1, ['App].[Master Book Name], ['App].[App Book Name], ['App].[Secondary App Book Name], ['App].[App Code], ['App].[App Book Status], ['App].[Book Transit], ['App].[Transit Desc], ['App].[Legal Entity Id], ['App].[Legal Entity Desc]
FROM ['App] INNER JOIN getRollpHierRpt ON ['App].[Book Transit] = getRollpHierRpt.Field1;

The examples i have seen on internet include getting a connection to Access/SQL server but if the datasets are two excel workbooks?我在互联网上看到的例子包括连接到 Access/SQL 服务器,但如果数据集是两个 excel 工作簿?

Sub MergeIt()
Dim conn1 As New ADODB.Connection
Dim conn2 As New ADODB.Connection
 With conn1
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\Users\amzubaid\Desktop\Practice Merging\13-10-2017.xlsm;" & 
"Extended Properties=""Excel 12.0 Macro;HDR=YES';IMEX=1"""
.Open
  End With
With conn2
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source=C:\Users\amzubaid\Desktop\Practice Merging\20181015-Practice.xlsm;" & "Extended Properties=""Excel 12.0 Macro;HDR=YES';IMEX=1"""
  .Open
 End With
Dim rsSmall As New Recordset
rsSmall.Open "select [Field1] from [getRollpHierRpt$]", conn1
 Dim r As Range
Dim rsBig As New Recordset
Do
Set r = Range("a" & Rows.Count).End(xlUp).Offset(1, 0) 'first unoccupied cell

rsBig.Open "select [App Code],[Legal Entity Desc]," & rsSmall("Field1") & " 
 As GetRollPH from [getRollpHierRpt$] where [BookTransit] = " & 
 rsSmall("Field1"), conn2
 r.CopyFromRecordset rsBig
rsBig.Close
rsSmall.MoveNext
Loop Until rsSmall.EOF  'end if file
rsSmall.Close
conn1.Close
conn2.Close

End Sub

You will need to set a connection to each workbook.您需要设置与每个工作簿的连接。 In effect this relies on treating each workbook as a table in a database.实际上,这依赖于将每个工作簿视为数据库中的一个表。 You must have column headings in each workbook, which are treated as field names.每个工作簿中都必须有列标题,它们被视为字段名称。 Try this for details试试这个了解详情

OK So it doesn't seem possible - so one possible workaround is to open the smaller table and then step through it and use the individual values to retrieve the desired records from the larger table: Here's an example using two workbooks called Testfile1 and testfile2.好的 所以这似乎不可能 - 所以一种可能的解决方法是打开较小的表,然后单步执行它并使用各个值从较大的表中检索所需的记录:这是一个使用名为 Testfile1 和 testfile2 的工作簿的示例。 Each contains a sheet called "test" and some data with column headings in testfile2 called "TestC" and "TestD" and the first (linking) filed in testfile1 called "TestA".每个都包含一个名为“test”的工作表和一些在名为“TestC”和“TestD”的 testfile2 中带有列标题的数据,以及名为“TestA”的 testfile1 中的第一个(链接)文件。 (This requires a reference to Microsoft Active X Data Objects" library in VBE, Tools, References) (这需要引用 VBE、工具、参考中的 Microsoft Active X 数据对象”库)

Sub TwoFiles()
Dim conn1 As New ADODB.Connection
Dim conn2 As New ADODB.Connection
With conn1
    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\testfile1.xlsm;" & "Extended Properties='Excel 12.0 Macro;HDR=YES';"
    .Open
End With
With conn2
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\testfile2.xlsm;" & "Extended Properties='Excel 12.0 Macro;HDR=YES';"
.Open
End With
Dim rsSmall As New Recordset
rsSmall.Open "select [testA] from [test$]", conn1
Dim r As Range
Dim rsBig As New Recordset
Do
Set r = Range("a" & Rows.Count).End(xlUp).Offset(1, 0) 'first unoccupied cell

rsBig.Open "select [TestC],[TestD]," & rsSmall("testA") & " As GetRollPH from [test$] where [testC] = " & rsSmall("testA"), conn2
r.CopyFromRecordset rsBig
rsBig.Close
rsSmall.MoveNext
Loop Until rsSmall.EOF  'end if file
rsSmall.Close
conn1.Close
conn2.Close


End Sub

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

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