I am trying to write a connection string and SQL script to run a query in Excel to pull data from another Excel workbook. This is what I have currently:
Sub Test()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim sql As String
' Create the connection string.
sConnString = "provider=Microsoft.Jet.OLEDB.4.0;data source=" & _
"C:\Users\dblois\Desktop\Shareenas Report.xlsx" + ";Extended Properties=Excel 8.0;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
sql = "SELECT * FROM [Data$A1:AC73333]"
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute(sql)
' Check we have data.
If Not rs.EOF Then
' Transfer result.
Sheets(1).Range("A1").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
I keep getting the following when I try to open the connection. What is wrong with my connection string?
External table is not in the expected format
I found the Following and changed my code to be:
sConnString = "provider=Microsoft.Jet.OLEDB.12.0;data source=" & _
"C:\Users\dblois\Desktop\Shareenas Report.xlsx" + ";Extended Properties=Excel 12.0;"
Then I am getting the following error:
Provider cannot be found. It may not be property installed