Hi
I've got 2 excel files which i want to compare and find out how many records with the same code (Sedol) exist in both of them. In other words it is an equivalent of creating an inner join on 2 tables.
The problem is that my 2 tables(SHEETS :B5LEPF$,B5LIPF$) are in different workbooks.
I have found a link that shed some light on what i want to achieve : however when i use this code in my macro i get an error :
Unrecognized database format - pointing to the path of my 2nd database. Is it because the 2nd databse is an excel spreadsheet? Can this be used with excel at all?
Could anyone help me with the SQL statement that would eneable me to create a join between 2 tables from different datasources?
PS : i know that there are easier ways of doing this in excel but i want specifically do this via ADO for training purposes
thank you and here's the code :
Sub Test()
On Error GoTo errorhandler
Dim rs As ADODB.Recordset
Dim sConnect As String
Dim sSql As String
Dim sDbase1Path As String
Dim sTable1Name As String
Dim sCol1Name As String
Dim sDbase2Path As String
Dim sTable2Name As String
Dim sCol2Name As String
Set rs = New ADODB.Recordset
sDbase1Path = ThisWorkbook.Path & "\" & "Portfolio -Equity.xls"
sTable1Name = "[" & "B5LEPF$" & "]"
sCol1Name = "[" & "SEDOL CODE" & "]"
sDbase2Path = "[;Database=" & ThisWorkbook.Path & "\" & "Portfolio - INTL .xls" & "]."
sTable2Name = "[" & "B5LIPF$" & "]"
sCol2Name = "[" & "SEDOL CODE" & "]"
sSql = "SELECT * FROM " & sTable1Name & ","
sSql = sSql & sDbase2Path & sTable2Name
sSql = sSql & " " & "WHERE " & sTable1Name & "." & sCol1Name & "=" & sTable2Name & "." & sCol2Name
'MsgBox sSql
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDbase1Path & _
";Extended Properties=Excel 8.0;"
rs.Open sSql, sConnect, adOpenForwardOnly, adLockReadOnly
Range("A1").CopyFromRecordset rs
errorhandler:
Debug.Print Err.Number
Debug.Print Err.Description
End Sub
I've got 2 excel files which i want to compare and find out how many records with the same code (Sedol) exist in both of them. In other words it is an equivalent of creating an inner join on 2 tables.
The problem is that my 2 tables(SHEETS :B5LEPF$,B5LIPF$) are in different workbooks.
I have found a link that shed some light on what i want to achieve : however when i use this code in my macro i get an error :
Unrecognized database format - pointing to the path of my 2nd database. Is it because the 2nd databse is an excel spreadsheet? Can this be used with excel at all?
Could anyone help me with the SQL statement that would eneable me to create a join between 2 tables from different datasources?
PS : i know that there are easier ways of doing this in excel but i want specifically do this via ADO for training purposes
thank you and here's the code :
Sub Test()
On Error GoTo errorhandler
Dim rs As ADODB.Recordset
Dim sConnect As String
Dim sSql As String
Dim sDbase1Path As String
Dim sTable1Name As String
Dim sCol1Name As String
Dim sDbase2Path As String
Dim sTable2Name As String
Dim sCol2Name As String
Set rs = New ADODB.Recordset
sDbase1Path = ThisWorkbook.Path & "\" & "Portfolio -Equity.xls"
sTable1Name = "[" & "B5LEPF$" & "]"
sCol1Name = "[" & "SEDOL CODE" & "]"
sDbase2Path = "[;Database=" & ThisWorkbook.Path & "\" & "Portfolio - INTL .xls" & "]."
sTable2Name = "[" & "B5LIPF$" & "]"
sCol2Name = "[" & "SEDOL CODE" & "]"
sSql = "SELECT * FROM " & sTable1Name & ","
sSql = sSql & sDbase2Path & sTable2Name
sSql = sSql & " " & "WHERE " & sTable1Name & "." & sCol1Name & "=" & sTable2Name & "." & sCol2Name
'MsgBox sSql
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDbase1Path & _
";Extended Properties=Excel 8.0;"
rs.Open sSql, sConnect, adOpenForwardOnly, adLockReadOnly
Range("A1").CopyFromRecordset rs
errorhandler:
Debug.Print Err.Number
Debug.Print Err.Description
End Sub