简体   繁体   中英

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. The datasets are not in Access but separate workbooks and the SQL query for the two workbooks are as follows:

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?

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. 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". (This requires a reference to Microsoft Active X Data Objects" library in VBE, Tools, References)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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