ADO in Excel

shlobodon

New Member
Joined
Oct 15, 2015
Messages
44
I am looking to retrieve rows from an excel file, format the columns of these rows using SQL, then paste the rows into another excel file. I have been trying to use ADO to do this. Is ADO just for access databases, or would it be beneficial for what I am doing? If it would be beneficial, what should the provider be? If not beneficial, what is the best way to proceed?

Any input is appreciated!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Yes you can use ADO in MS-Excel to read other MS-Excel files. Here is a sample code:

Code:
Sub querySheetData()
    Dim path As String
    Dim cn As ADODB.Connection
    Dim rst As New ADODB.Recordset
    
    path = "C:\TestingResults\testfile.xlsx"
    
    Set cn = New ADODB.Connection
    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & path & ";Extended Properties=Excel 12.0 Xml;"
        .Open
    End With
    
    strQuery = "SELECT * FROM [Sheet1$] where testCol = 2"
    Set rst = cn.Execute(strQuery)

    ThisWorkbook.Worksheets("Cols").Range("B7").CopyFromRecordset rst

End Sub

Note: Set library reference (Tools->Reference in VBA window) to "Microsoft ActiveX Data Objects" library.
 
Last edited:
Upvote 0
Thank you! Your coding worked right away. Can you suggest any good resources on how you built the ConnectionString? I am new to programming. There seem to be many parameters associated with the connectionstring.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,743
Messages
6,126,603
Members
449,321
Latest member
syzer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top