How can I run SQL statements on a named range within an excel sheet?
You can just use the name.
Dim cn As ADODB.ConnectionDim rs As ADODB.Recordset strFile = Workbooks(1).FullNamestrCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _ & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"Set cn = CreateObject("ADODB.Connection")Set rs = CreateObject("ADODB.Recordset")cn.Open strCon''Pick one:strSQL = "SELECT * FROM DataTable" ''Named rangestrSQL = "SELECT * FROM [Sheet1$A1:E346]" ''Rangers.Open strSQL, cnDebug.Print rs.GetString
In response to question part 2
I notice that you only want today's records, so you should be able to modify the sql to:
strSQL = "SELECT DISTINCT(Expiration) FROM [PositionSummaryTable] " _& "where [Instrument Type] = 'LSTOPT' AND [Expiration]=#" _& Format(Date(),"yyyy/mm/dd") & "#"
You have not closed the connection:
cn.Close
And then
Set rs=Nothing Set cn=Nothing