Hello,

My questions are the following :

- I use VBA in Excel, and would like to use Excel itself as a data source (“Using ADO with Non-Standard Data Sources, Querying Microsoft Excel Workbooks”). I’d like to keep a table in excel and use SQL expression power to manipulate the data in the table (selection, grouping, filtering…), rather than using the usual Excel lookups.

- The table to query is located in the active workbook, as opposed to being saved in another, inactive, file.

- I then dump the SQL result in a range located in the Sheet1.

Here is the code I have:
VB Code:
  1. Sub QueryXlSheet()
  2.     Dim RS As ADODB.Recordset
  3.     Dim ConnectionString As String
  4.  
  5.     On Error GoTo ErrHandler
  6.     ConnectionString = _
  7.     "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  8.     "Data Source=" & ThisWorkbook.FullName & ";" & _
  9.     "Extended Properties=Excel 8.0;"
  10.  
  11.     Dim SQL As String
  12.     Dim TESTRNG As Range
  13. 'TESTRNG is an excel range name, which defines the table to query,
  14. 'with field names in the first, header row, and records in other rows.
  15.     Set TESTRNG = ActiveSheet.Range("A1:C6")
  16.     SQL = "SELECT * FROM TESTRNG;"
  17.     Set RS = New ADODB.Recordset
  18.     Call RS.Open(SQL, ConnectionString, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText)
  19.     Debug.Print RS.RecordCount
  20.  
  21. ErrHandler:
  22.     Debug.Print Err.Description
  23.     If (RS.State = ObjectStateEnum.adStateOpen) Then
  24.         RS.Close
  25.     End If
  26.     Set RS = Nothing
  27. End Sub
I am getting the following error:

The Microsoft Jet database engine could not find the object 'TESTRNG'. Make sure the object exists and that you spell its name and the path name correctly.

I do not know what went wrong. It could be great if you could put me on the right tracks.

Thanks in advance.