In VBA I want to query one Excel table using SQL statements and get the results to another Excel table. I need some direction to what to read because I am confused. Excuse me for asking many different questions in one post at once, but the questions cover one subject.
The source table. How should I find out if the source data is a query table or ListObject table (excuse me for ignorance, if calling it ListObjects table is not a proper way). What is the difference between QueryTables and ListObjects tables? This explanation has not shed light on it.
How to get the proper connection string to existing table (be it a QueryTable or ListObject table)?
What is the benefit of using ADO to do the job? If I get the idea with ADO, we make a self connection to the source table in Excel (correct me if I am wrong). This link Query Tables (QueryTables) in Excel 2010 with VBA with VBA creating many connections shows that ADO is not necessery, while this one argues that using it is good: http://itknowledgeexchange.techtarget.com/beyond-excel/say-goodbye-to-querytables/
The code below (grabbed from here) looks simple and promissing:
Sub idee()
ActiveWorkbook.Sheets.Add
With ActiveSheet.QueryTables.Add(“ODBC;DSN=Excel-bestanden;DBQ=E:TheDatabook.xls”,[H1])
.CommandText = “SELECT name, number FROM TheData WHERE number =1″
.Refresh False
End With
End Sub
However I would like to use there proper connection string (replacing this: ODBC;DSN=Excel-bestanden;DBQ=E:TheDatabook.xls) and replace named range TheData with QueryTable or ListObject table and I would like to get the output to QueryTable or ListObject table.