A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Hi Guys,
Thanks for your responses, however,
One workbook, two worksheets, easy and have done many time, whether the active workbook, or a file on the PC. I am looking for a solution for joining an active workbook, with a data range named, to a file on my PC also with a data range named. An example follows, but with txt files. These like the one workbook, two worksheets are easy, in the case of the text files, because the structure of the connect string only references the folder containing the data files, and the schema.ini file contains the file structures. I have many examples of this as well.
Example of what I want using Excel, not txt or csv files. Note the file names for the two text files appears in the last segment where the join occurs.
With Sheets("Customer").QueryTables.Add(Connection:=Array(Array( _
"ODBC;DefaultDir=" & ActiveWorkbook.Path & ";Driver={Microsoft Text Dr" _
), Array( _
"iver (*.txt; *.csv)};DriverId=27;Extensions=txt,csv,tab,asc;FIL=text;MaxBufferSize=2048;MaxScanRows=25;PageTimeout=5;SafeTransa" _
), Array("ctions=0;Threads=3;UID=admin;UserCommitSync=Yes;")), Destination:= _
Sheets("Customer").Range("A1"))
.CommandText = Array( _
"SELECT Customers.Customer, Customers.NAME 1, Customers.NAME 2" _
, _
", Customers.STREET, Customers.CITY, Customers.POSTALCODE," _
, _
"Finance.CREDIT LIMIT, Finance.EXPOSURE" & Chr(13) & "" & Chr(10) & "FROM Customers.txt Customers INNER JOIN " & _
" Finance.csv Finance ON CUSTOMERS.Customer = Finance.Customer")
The code I am looking for, if it exists, must be suitable to be run in an Add-in with the rest of what I am doing.
Edit 02/03/2014
I have given up on there being a solution for joining two Excel workbooks, and gone the clunky way of copying the data set onto a new worksheet in the active workbook. I can then use the two named data ranges in the active workbook to run the SQL UPDATE function. While this is A solution, it is not as elegant as being able to do it with two separate workbooks.
Routine to open ADO connection:
Sub ConnectToExcelPivot()
On Error Resume Next
strFileToStartWith = ActiveWorkbook.Path & "" & ActiveWorkbook.Name
Set mExcelConn = New ADODB.Connection
mExcelConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" _
& strFileToStartWith & ";" _
& "Extended Properties=Excel 8.0;"
If Err.Number <> 0 Then
mExcelConn.ConnectionString = "Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" _
& strFileToStartWith & ";" _
& "Extended Properties=Excel 12.0 Xml;"
End If
On Error GoTo 0
mExcelConn.Open
Set mExcelCmd = New ADODB.Command
Set mExcelCmd.ActiveConnection = mExcelConn
mExcelCmd.CommandType = adCmdText
End Sub
Excerpt from code:
strSQL = "UPDATE data1 INNER JOIN Data2 " _
& "ON data1.RenewalCheck = Data2.Key SET " _
& "Data2.Old_ExpiryDate = [data1]![ExpiryDate];"
'Set command text to SQL statement
mExcelCmd.CommandText = strSQL
On Error Resume Next
'Execute SQL statement
Set mExcelRst = mExcelCmd.Execute
On Error GoTo 0
'Close connection to .xls workbook
DisconnectFromExcel
End Excerpt from code
Routine to close ADO connections:
Sub DisconnectFromExcel()
On Error Resume Next
Set mExcelCmd = Nothing
mExcelConn.Close
Set mExcelConn = Nothing
End Sub
This saves using VLOOKUP, updating one row at a time via a loop and ADO connection to the data source workbook and keeps the code available to a once off data set using an Add-In approach.
Please mark this question answered and close the thread, thanks.